Short answer: in Jetpack Compose with Material 3, you create an app bar with TopAppBar in the topBar slot of a Scaffold. Put buttons in actions, a back or menu icon in navigationIcon, and show an overflow menu by combining an IconButton with a DropdownMenu.
TopAppBar with actions and an overflow menu
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AppBarWithMenu() {
var menuExpanded by remember { mutableStateOf(false) }
Scaffold(
topBar = {
TopAppBar(
title = { Text("Codeplayon") },
navigationIcon = {
IconButton(onClick = { /* go back or open drawer */ }) {
Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = "Back")
}
},
actions = {
IconButton(onClick = { /* search */ }) {
Icon(Icons.Default.Search, contentDescription = "Search")
}
Box {
IconButton(onClick = { menuExpanded = true }) {
Icon(Icons.Default.MoreVert, contentDescription = "More options")
}
DropdownMenu(
expanded = menuExpanded,
onDismissRequest = { menuExpanded = false }
) {
DropdownMenuItem(
text = { Text("Settings") },
onClick = {
menuExpanded = false
// open settings
}
)
DropdownMenuItem(
text = { Text("About") },
onClick = {
menuExpanded = false
// open about
}
)
}
}
}
)
}
) { innerPadding ->
Box(
modifier = Modifier.fillMaxSize().padding(innerPadding),
contentAlignment = Alignment.Center
) {
Text("Screen content")
}
}
}
Notes:
- The
DropdownMenuis inside aBoxwith the button, so the menu opens right below the icon. Icons.AutoMirrored.Filled.ArrowBackis the current name. On older Compose versions useIcons.Filled.ArrowBack.- Always use
innerPaddingfrom the Scaffold, or content hides behind the bar. - On some Material 3 versions, TopAppBar needs
@OptIn(ExperimentalMaterial3Api::class).
Change the colors
TopAppBar(
title = { Text("Codeplayon") },
colors = TopAppBarDefaults.topAppBarColors(
containerColor = MaterialTheme.colorScheme.primary,
titleContentColor = MaterialTheme.colorScheme.onPrimary,
navigationIconContentColor = MaterialTheme.colorScheme.onPrimary,
actionIconContentColor = MaterialTheme.colorScheme.onPrimary
)
)
Other app bar types
| Composable | Use it for |
|---|---|
TopAppBar |
Standard bar with a left-aligned title |
CenterAlignedTopAppBar |
Title centered |
MediumTopAppBar |
Taller bar that collapses on scroll |
LargeTopAppBar |
Large title that collapses to a small bar |
Collapse the bar when the user scrolls
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CollapsingAppBarScreen() {
val scrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior()
Scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = {
LargeTopAppBar(
title = { Text("Articles") },
scrollBehavior = scrollBehavior
)
}
) { innerPadding ->
LazyColumn(contentPadding = innerPadding) {
items(50) { index ->
Text("Item " + (index + 1), Modifier.padding(16.dp))
}
}
}
}
The scroll behavior connects the list scrolling to the bar. For lists, see our LazyColumn guide. For a drawer menu button in the app bar, see navigation drawer in Compose.
Common mistakes
- Placing the DropdownMenu outside the Box with the icon. The menu can appear in the wrong spot.
- Not closing the menu in
onClick. SetmenuExpanded = falseafter the user chooses an item. - Using Material 2 imports. Import
androidx.compose.material3.*for these Material 3 APIs, notandroidx.compose.material.*. - Skipping content descriptions. Add them to icons for screen readers.
FAQ
How do I add a menu to the top app bar in Jetpack Compose?
Add an IconButton and a DropdownMenu inside the actions slot, controlled by a boolean state.
How do I add a back button?
Set the navigationIcon with an IconButton that calls your back handler, such as navController.popBackStack().
How do I change the app bar color?
Use TopAppBarDefaults.topAppBarColors(...) and set containerColor and the content colors.
How do I make a collapsing app bar?
Use LargeTopAppBar or MediumTopAppBar with exitUntilCollapsedScrollBehavior and nestedScroll.
More: Jetpack Compose tutorials.