Navigation UI components for implementing app navigation patterns, including top app bars and bottom navigation bars. These components provide consistent navigation experiences following Material3 design principles.
import androidx.compose.material3.*
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.NavigationRail
import androidx.compose.material3.ExperimentalMaterial3ApiPrimary app bar displayed at the top of screens for navigation and screen-level actions.
/**
* Material3 top app bar for screen titles, navigation, and actions
* @param title The title content displayed in the app bar
* @param modifier Modifier to be applied to the app bar
* @param navigationIcon Optional navigation icon (typically back button or menu)
* @param actions Row of action icons displayed at the end of the app bar
* @param windowInsets Window insets for proper edge-to-edge layout
* @param colors Color scheme for the app bar in different states
* @param scrollBehavior Optional scroll behavior for app bar animations
*/
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TopAppBar(
title: @Composable () -> Unit,
modifier: Modifier = Modifier,
navigationIcon: @Composable () -> Unit = {},
actions: @Composable RowScope.() -> Unit = {},
windowInsets: WindowInsets = TopAppBarDefaults.windowInsets,
colors: TopAppBarColors = TopAppBarDefaults.topAppBarColors(),
scrollBehavior: TopAppBarScrollBehavior? = null
)Usage Examples:
// Basic top app bar
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun BasicTopAppBar() {
TopAppBar(
title = { Text("Screen Title") }
)
}
// Top app bar with navigation and actions
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun FullTopAppBar() {
TopAppBar(
title = { Text("My App") },
navigationIcon = {
IconButton(onClick = { /* handle navigation */ }) {
Icon(Icons.Default.ArrowBack, contentDescription = "Back")
}
},
actions = {
IconButton(onClick = { /* handle search */ }) {
Icon(Icons.Default.Search, contentDescription = "Search")
}
IconButton(onClick = { /* handle menu */ }) {
Icon(Icons.Default.MoreVert, contentDescription = "More")
}
}
)
}
// Top app bar with custom colors
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CustomColorTopAppBar() {
TopAppBar(
title = { Text("Custom Colors") },
colors = TopAppBarDefaults.topAppBarColors(
containerColor = MaterialTheme.colorScheme.primaryContainer,
titleContentColor = MaterialTheme.colorScheme.onPrimaryContainer
)
)
}Medium-sized top app bar with expanded height for prominent titles.
/**
* Material3 medium top app bar with expanded height for prominent screen titles
* @param title The title content displayed in the app bar
* @param modifier Modifier to be applied to the app bar
* @param navigationIcon Optional navigation icon
* @param actions Row of action icons displayed at the end of the app bar
* @param windowInsets Window insets for proper edge-to-edge layout
* @param colors Color scheme for the app bar in different states
* @param scrollBehavior Optional scroll behavior for app bar animations
*/
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MediumTopAppBar(
title: @Composable () -> Unit,
modifier: Modifier = Modifier,
navigationIcon: @Composable () -> Unit = {},
actions: @Composable RowScope.() -> Unit = {},
windowInsets: WindowInsets = TopAppBarDefaults.windowInsets,
colors: TopAppBarColors = TopAppBarDefaults.mediumTopAppBarColors(),
scrollBehavior: TopAppBarScrollBehavior? = null
)Large-sized top app bar with maximum height for hero-style titles.
/**
* Material3 large top app bar with maximum height for hero-style screen titles
* @param title The title content displayed in the app bar
* @param modifier Modifier to be applied to the app bar
* @param navigationIcon Optional navigation icon
* @param actions Row of action icons displayed at the end of the app bar
* @param windowInsets Window insets for proper edge-to-edge layout
* @param colors Color scheme for the app bar in different states
* @param scrollBehavior Optional scroll behavior for app bar animations
*/
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun LargeTopAppBar(
title: @Composable () -> Unit,
modifier: Modifier = Modifier,
navigationIcon: @Composable () -> Unit = {},
actions: @Composable RowScope.() -> Unit = {},
windowInsets: WindowInsets = TopAppBarDefaults.windowInsets,
colors: TopAppBarColors = TopAppBarDefaults.largeTopAppBarColors(),
scrollBehavior: TopAppBarScrollBehavior? = null
)Bottom navigation bar for primary navigation between top-level destinations.
/**
* Material3 bottom navigation bar for primary navigation between destinations
* @param modifier Modifier to be applied to the navigation bar
* @param containerColor Background color of the navigation bar
* @param contentColor Default content color for the navigation bar
* @param tonalElevation Tonal elevation for color tinting
* @param windowInsets Window insets for proper edge-to-edge layout
* @param content Row content containing NavigationBarItem components
*/
@Composable
fun NavigationBar(
modifier: Modifier = Modifier,
containerColor: Color = NavigationBarDefaults.containerColor,
contentColor: Color = MaterialTheme.colorScheme.onSurfaceVariant,
tonalElevation: Dp = NavigationBarDefaults.Elevation,
windowInsets: WindowInsets = NavigationBarDefaults.windowInsets,
content: @Composable RowScope.() -> Unit
)Usage Examples:
// Basic navigation bar
@Composable
fun BottomNavigation() {
var selectedItem by remember { mutableIntStateOf(0) }
val items = listOf("Home", "Search", "Profile")
val icons = listOf(Icons.Default.Home, Icons.Default.Search, Icons.Default.Person)
NavigationBar {
items.forEachIndexed { index, item ->
NavigationBarItem(
icon = { Icon(icons[index], contentDescription = item) },
label = { Text(item) },
selected = selectedItem == index,
onClick = { selectedItem = index }
)
}
}
}
// Navigation bar with custom colors
@Composable
fun CustomNavigationBar() {
NavigationBar(
containerColor = MaterialTheme.colorScheme.surfaceVariant,
contentColor = MaterialTheme.colorScheme.onSurfaceVariant
) {
NavigationBarItem(
icon = { Icon(Icons.Default.Home, contentDescription = "Home") },
label = { Text("Home") },
selected = true,
onClick = { }
)
NavigationBarItem(
icon = { Icon(Icons.Default.Settings, contentDescription = "Settings") },
label = { Text("Settings") },
selected = false,
onClick = { }
)
}
}Individual item within a NavigationBar for destination selection.
/**
* Material3 navigation bar item for individual destinations in bottom navigation
* @param selected Whether this item is currently selected
* @param onClick Callback invoked when the item is clicked
* @param icon Icon content for the navigation item
* @param modifier Modifier to be applied to the item
* @param enabled Whether the item is enabled for interaction
* @param label Optional text label displayed below the icon
* @param alwaysShowLabel Whether to always show the label or only when selected
* @param colors Color scheme for the item in different states
* @param interactionSource Source for tracking interaction states
*/
@Composable
fun NavigationBarItem(
selected: Boolean,
onClick: () -> Unit,
icon: @Composable () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
label: (@Composable () -> Unit)? = null,
alwaysShowLabel: Boolean = true,
colors: NavigationBarItemColors = NavigationBarItemDefaults.colors(),
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }
)Usage Examples:
// Basic navigation bar item
NavigationBarItem(
selected = true,
onClick = { /* handle click */ },
icon = { Icon(Icons.Default.Home, contentDescription = "Home") },
label = { Text("Home") }
)
// Navigation bar item with badge
NavigationBarItem(
selected = false,
onClick = { /* handle click */ },
icon = {
BadgedBox(
badge = { Badge { Text("3") } }
) {
Icon(Icons.Default.Notifications, contentDescription = "Notifications")
}
},
label = { Text("Notifications") }
)
// Navigation bar item with custom colors
NavigationBarItem(
selected = true,
onClick = { /* handle click */ },
icon = { Icon(Icons.Default.Favorite, contentDescription = "Favorites") },
label = { Text("Favorites") },
colors = NavigationBarItemDefaults.colors(
selectedIconColor = MaterialTheme.colorScheme.primary,
selectedTextColor = MaterialTheme.colorScheme.primary
)
)Vertical navigation component for tablets and desktop layouts.
/**
* Material3 navigation rail for vertical navigation on larger screens
* @param modifier Modifier to be applied to the navigation rail
* @param containerColor Background color of the navigation rail
* @param contentColor Default content color for the navigation rail
* @param header Optional header content displayed at the top
* @param windowInsets Window insets for proper edge-to-edge layout
* @param content Column content containing NavigationRailItem components
*/
@Composable
fun NavigationRail(
modifier: Modifier = Modifier,
containerColor: Color = NavigationRailDefaults.ContainerColor,
contentColor: Color = contentColorFor(containerColor),
header: (@Composable () -> Unit)? = null,
windowInsets: WindowInsets = NavigationRailDefaults.windowInsets,
content: @Composable ColumnScope.() -> Unit
)Individual item within a NavigationRail for destination selection.
/**
* Material3 navigation rail item for individual destinations in vertical navigation
* @param selected Whether this item is currently selected
* @param onClick Callback invoked when the item is clicked
* @param icon Icon content for the navigation item
* @param modifier Modifier to be applied to the item
* @param enabled Whether the item is enabled for interaction
* @param label Optional text label displayed below the icon
* @param alwaysShowLabel Whether to always show the label
* @param colors Color scheme for the item in different states
* @param interactionSource Source for tracking interaction states
*/
@Composable
fun NavigationRailItem(
selected: Boolean,
onClick: () -> Unit,
icon: @Composable () -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
label: (@Composable () -> Unit)? = null,
alwaysShowLabel: Boolean = true,
colors: NavigationRailItemColors = NavigationRailItemDefaults.colors(),
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }
)Default configurations for top app bars.
object TopAppBarDefaults {
/** Default window insets for top app bars */
val windowInsets: WindowInsets
/**
* Creates default colors for small top app bars
* @param containerColor Background color of the app bar
* @param scrolledContainerColor Background color when scrolled
* @param navigationIconContentColor Color of the navigation icon
* @param titleContentColor Color of the title text
* @param actionIconContentColor Color of action icons
*/
fun topAppBarColors(
containerColor: Color = Color.Unspecified,
scrolledContainerColor: Color = Color.Unspecified,
navigationIconContentColor: Color = Color.Unspecified,
titleContentColor: Color = Color.Unspecified,
actionIconContentColor: Color = Color.Unspecified,
): TopAppBarColors
/**
* Creates default colors for medium top app bars
*/
fun mediumTopAppBarColors(
containerColor: Color = Color.Unspecified,
scrolledContainerColor: Color = Color.Unspecified,
navigationIconContentColor: Color = Color.Unspecified,
titleContentColor: Color = Color.Unspecified,
actionIconContentColor: Color = Color.Unspecified,
): TopAppBarColors
/**
* Creates default colors for large top app bars
*/
fun largeTopAppBarColors(
containerColor: Color = Color.Unspecified,
scrolledContainerColor: Color = Color.Unspecified,
navigationIconContentColor: Color = Color.Unspecified,
titleContentColor: Color = Color.Unspecified,
actionIconContentColor: Color = Color.Unspecified,
): TopAppBarColors
}Default configurations for navigation bars.
object NavigationBarDefaults {
/** Default container color for navigation bars */
val containerColor: Color
@Composable get
/** Default elevation for navigation bars */
val Elevation: Dp
/** Default window insets for navigation bars */
val windowInsets: WindowInsets
}Default configurations for navigation bar items.
object NavigationBarItemDefaults {
/**
* Creates default colors for navigation bar items
* @param selectedIconColor Color of icon when selected
* @param selectedTextColor Color of text when selected
* @param indicatorColor Color of selection indicator
* @param unselectedIconColor Color of icon when not selected
* @param unselectedTextColor Color of text when not selected
* @param disabledIconColor Color of icon when disabled
* @param disabledTextColor Color of text when disabled
*/
fun colors(
selectedIconColor: Color = Color.Unspecified,
selectedTextColor: Color = Color.Unspecified,
indicatorColor: Color = Color.Unspecified,
unselectedIconColor: Color = Color.Unspecified,
unselectedTextColor: Color = Color.Unspecified,
disabledIconColor: Color = Color.Unspecified,
disabledTextColor: Color = Color.Unspecified,
): NavigationBarItemColors
}Default configurations for navigation rails.
object NavigationRailDefaults {
/** Default container color for navigation rails */
val ContainerColor: Color
@Composable get
/** Default window insets for navigation rails */
val windowInsets: WindowInsets
}Default configurations for navigation rail items.
object NavigationRailItemDefaults {
/**
* Creates default colors for navigation rail items
* @param selectedIconColor Color of icon when selected
* @param selectedTextColor Color of text when selected
* @param indicatorColor Color of selection indicator
* @param unselectedIconColor Color of icon when not selected
* @param unselectedTextColor Color of text when not selected
* @param disabledIconColor Color of icon when disabled
* @param disabledTextColor Color of text when disabled
*/
fun colors(
selectedIconColor: Color = Color.Unspecified,
selectedTextColor: Color = Color.Unspecified,
indicatorColor: Color = Color.Unspecified,
unselectedIconColor: Color = Color.Unspecified,
unselectedTextColor: Color = Color.Unspecified,
disabledIconColor: Color = Color.Unspecified,
disabledTextColor: Color = Color.Unspecified,
): NavigationRailItemColors
}interface TopAppBarColors {
/** Container color of the top app bar */
val containerColor: Color
/** Container color when content is scrolled */
val scrolledContainerColor: Color
/** Color of the navigation icon */
val navigationIconContentColor: Color
/** Color of the title content */
val titleContentColor: Color
/** Color of action icons */
val actionIconContentColor: Color
}interface TopAppBarScrollBehavior {
/** Current scroll state */
val state: TopAppBarState
/** Whether the app bar is pinned */
val isPinned: Boolean
/** Snap animation spec */
val snapAnimationSpec: AnimationSpec<Float>?
/** Fling animation spec */
val flingAnimationSpec: DecayAnimationSpec<Float>?
}interface NavigationBarItemColors {
/**
* Represents the icon color for this item, depending on whether it's [selected].
* @param selected whether the item is selected
*/
fun iconColor(selected: Boolean): Color
/**
* Represents the text color for this item, depending on whether it's [selected].
* @param selected whether the item is selected
*/
fun textColor(selected: Boolean): Color
/**
* Represents the indicator color for this item, depending on whether it's [selected].
* @param selected whether the item is selected
*/
fun indicatorColor(selected: Boolean): Color
}interface NavigationRailItemColors {
/**
* Represents the icon color for this item, depending on whether it's [selected].
* @param selected whether the item is selected
*/
fun iconColor(selected: Boolean): Color
/**
* Represents the text color for this item, depending on whether it's [selected].
* @param selected whether the item is selected
*/
fun textColor(selected: Boolean): Color
/**
* Represents the indicator color for this item, depending on whether it's [selected].
* @param selected whether the item is selected
*/
fun indicatorColor(selected: Boolean): Color
}