Android Navigation Bottom Sheet — Complete Guide
In this tutorial, you'll learn about Android Navigation Bottom Sheet. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
Your bottom sheet doesn't expand to full height, the navigation inside the sheet doesn't work, or dragging dismisses the sheet accidentally.
Wrong Approach ❌
// Bare BottomSheetDialogFragment without proper container
class MyBottomSheet : BottomSheetDialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_sheet, container, false)
}
}
<!-- No CoordinatorLayout — sheet can't drag properly -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:text="Content" />
</FrameLayout>
Output: Bottom sheet doesn't respond to drag gestures. Peek height doesn't work.
Right Approach ✅
class MyBottomSheet : BottomSheetDialogFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
return inflater.inflate(R.layout.fragment_sheet, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Set peek height programmatically
dialog?.let { dialog ->
val sheet = dialog.findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
sheet?.layoutParams?.height = ViewGroup.LayoutParams.MATCH_PARENT
}
// Prevent accidental dismiss on drag down
isCancelable = true
setStyle(STYLE_NORMAL, R.style.ThemeOverlay_Material3_BottomSheetDialog)
}
// Navigation inside bottom sheet
private fun navigateToSubScreen() {
val navController = findNavController()
// Use a nested NavHostFragment inside the sheet if needed
}
}
// With ViewPager2 inside bottom sheet
class TabbedBottomSheet : BottomSheetDialogFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val adapter = ViewPagerAdapter(this)
view.findViewById<ViewPager2>(R.id.viewPager).adapter = adapter
}
}
Output: Bottom sheet expands to full height, handles navigation correctly.
Prevention
- Use
BottomSheetDialogFragmentfor content with drag-to-dismiss. - Set
MATCH_PARENTon the bottom sheet container for full height. - Use
NestedScrollViewinside bottom sheet for scrollable content. - For navigation, use a nested
NavHostFragmentinside the sheet.
Common Mistakes with navigation bottom sheet
- Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
These mistakes appear frequently in real-world Android code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro