Skip to content

Jetpack Compose Drawer — Complete Guide

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Jetpack Compose Drawer. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

Your drawer opens on a swipe but the hamburger icon doesn't reflect the state, or the drawer content overlaps the main content.

Wrong Approach ❌

@Composable
fun BadDrawer() {
    // No drawer state — always closed
    ModalNavigationDrawer(drawerContent = { Text("Drawer") }) {
        // Main content
    }
}

@Composable
fun GestureConflict() {
    ModalNavigationDrawer(
        drawerContent = { Text("Drawer") },
        gesturesEnabled = true
    ) {
        LazyColumn { items(50) { Text("Item $it") } } // Scroll conflicts with drawer
    }
}

Output: Drawer never opens or opens accidentally during list scroll.

Right Approach ✅

@Composable
fun GoodDrawer() {
    val drawerState = rememberDrawerState(DrawerValue.Closed)
    val scope = rememberCoroutineScope()
    val isOpen by remember { derivedStateOf { drawerState.isOpen } }

    ModalNavigationDrawer(
        drawerState = drawerState,
        gesturesEnabled = drawerState.isOpen || true,
        drawerContent = {
            ModalDrawerSheet {
                // Drawer header
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(180.dp)
                        .background(MaterialTheme.colorScheme.primaryContainer)
                ) {
                    Column(modifier = Modifier.align(Alignment.BottomStart).padding(16.dp)) {
                        Text("User Name", style = MaterialTheme.typography.headlineSmall)
                        Text("email@example.com", style = MaterialTheme.typography.bodyMedium)
                    }
                }

                // Navigation items
                NavigationDrawerItem(label = { Text("Home") }, selected = true, onClick = {
                    scope.launch { drawerState.close() }
                })
                NavigationDrawerItem(label = { Text("Settings") }, selected = false, onClick = {
                    scope.launch { drawerState.close() }
                })
            }
        }
    ) {
        Scaffold(
            topBar = {
                TopAppBar(
                    title = { Text("App") },
                    navigationIcon = {
                        IconButton(onClick = {
                            scope.launch {
                                if (drawerState.isClosed) drawerState.open()
                                else drawerState.close()
                            }
                        }) {
                            Icon(Icons.Default.Menu, "Menu")
                        }
                    }
                )
            }
        ) { padding ->
            MainContent(Modifier.padding(padding))
        }
    }
}

Output: Drawer opens on hamburger click or gesture, state synced with icon.

Prevention

  • Use rememberDrawerState() with DrawerValue.Closed initial state.
  • Use ModalDrawerSheet for drawer content with proper styling.
  • Handle hamburger icon toggling via drawerState.isOpen.
  • Disable gesturesEnabled when scrollable content is in the drawer.

Common Mistakes with compose drawer

  1. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  2. Using return to exit a function early instead of wrapping a pure value in the monad
  3. Mixing let bindings with <- bindings in do notation, producing type errors

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

### What is the difference between ModalNavigationDrawer and PermanentNavigationDrawer?

ModalNavigationDrawer overlays on top of content (common on phones). PermanentNavigationDrawer sits beside content (common on tablets). Choose based on screen size.

### How do I close the drawer when an item is selected?

Use scope.launch { drawerState.close() } inside the onClick callback of NavigationDrawerItem. Use rememberCoroutineScope() to get the scope.

### Can I customize the drawer open/close animation?

The animation is built-in and uses Material motion. For custom animations, use DrawerState with animateTo(DrawerValue.Open) or snapTo().

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro