Navigation Drawer in Jetpack Compose (Material 3)

Short answer: in Jetpack Compose with Material 3, wrap your screen in ModalNavigationDrawer, put a ModalDrawerSheet with NavigationDrawerItems in its drawerContent, and open or close it with a DrawerState inside a coroutine.

This tutorial builds a complete drawer with a top app bar, a menu button, selectable items and closing on click.

Complete example

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DrawerScreen() {
    val drawerState = rememberDrawerState(DrawerValue.Closed)
    val scope = rememberCoroutineScope()
    var selected by remember { mutableStateOf("Home") }
    val items = listOf("Home", "Profile", "Settings")

    ModalNavigationDrawer(
        drawerState = drawerState,
        drawerContent = {
            ModalDrawerSheet {
                Text(
                    text = "Codeplayon",
                    modifier = Modifier.padding(16.dp),
                    style = MaterialTheme.typography.titleLarge
                )
                HorizontalDivider()
                items.forEach { item ->
                    NavigationDrawerItem(
                        label = { Text(item) },
                        selected = item == selected,
                        onClick = {
                            selected = item
                            scope.launch { drawerState.close() }
                        },
                        modifier = Modifier.padding(NavigationDrawerItemDefaults.ItemPadding)
                    )
                }
            }
        }
    ) {
        Scaffold(
            topBar = {
                TopAppBar(
                    title = { Text(selected) },
                    navigationIcon = {
                        IconButton(onClick = { scope.launch { drawerState.open() } }) {
                            Icon(Icons.Default.Menu, contentDescription = "Open menu")
                        }
                    }
                )
            }
        ) { padding ->
            Box(
                modifier = Modifier.fillMaxSize().padding(padding),
                contentAlignment = Alignment.Center
            ) {
                Text("Screen: $selected")
            }
        }
    }
}

Call DrawerScreen() from setContent { MaterialTheme { DrawerScreen() } }. On older Material 3 versions, replace HorizontalDivider() with Divider().

How it works

  • rememberDrawerState stores whether the drawer is open or closed.
  • drawerState.open() and close() are suspend functions, so call them from scope.launch { }.
  • NavigationDrawerItem shows a label, a selected state and a click action.
  • Scaffold and TopAppBar provide the screen content and the menu button.

Connect the drawer to navigation

To open real screens, call navController.navigate(route) in the item’s onClick, then close the drawer. Put your NavHost inside the Scaffold content. See our guide to Jetpack Compose navigation for the setup.

Other drawer types

  • ModalNavigationDrawer: slides over the content and is the common choice on phones.
  • PermanentNavigationDrawer: always visible, good for large screens and tablets.
  • DismissibleNavigationDrawer: sits beside the content and can be toggled on larger screens.

Common mistakes

  • Calling drawerState.open() outside a coroutine. It is a suspend function. Use rememberCoroutineScope().
  • Not closing the drawer after a click. Users expect it to close after choosing an item.
  • Keeping selection in the wrong place. For real navigation, derive the selected item from the current back stack entry.
  • Missing the opt-in. TopAppBar may need @OptIn(ExperimentalMaterial3Api::class) on some versions.

FAQ

How do I open the navigation drawer from a button?

Call scope.launch { drawerState.open() } in the button’s click handler.

What replaces DrawerLayout in Compose?

ModalNavigationDrawer replaces the View system’s DrawerLayout.

Can I use a drawer with bottom navigation?

Yes, but pick one main navigation pattern per level. Use a drawer for secondary destinations and a bottom bar for top-level tabs.

More: Jetpack Compose tutorials.