AppBarRow
Common
Component in Material 3 Compose
An [AppBarRow] arranges its children in a horizontal sequence, and if any children overflow the constraints, an overflow indicator is displayed.
This composable lays out its children from left to right in LTR layouts and from right to left in RTL layouts. If the children's combined width exceeds the available width, [overflowIndicator] is displayed at the end of the row, replacing the content that otherwise cannot fit. The items are constructed through a DSL in [AppBarRowScope]. Each item provides a way to render itself in the row layout, and an alternative way, to render inside of a dropdown menu, when there is overflow.
A sample with an [TopAppBar], that varies the number of actions shown.
Last updated:
Installation
dependencies {
implementation("androidx.compose.material3:material3:1.4.0-alpha15")
}
Overloads
@Composable
@Suppress("ComposableLambdaParameterPosition", "KotlinDefaultParameterOrder")
fun AppBarRow(
overflowIndicator: @Composable (AppBarMenuState) -> Unit,
modifier: Modifier = Modifier,
maxItemCount: Int = Int.MAX_VALUE,
content: AppBarRowScope.() -> Unit,
)
Parameters
name | description |
---|---|
overflowIndicator | A composable that is displayed at the end of the row when the content overflows. It receives an [AppBarMenuState] instance. |
modifier | The modifier to be applied to the row. |
maxItemCount | the max amount of items that should render in the row, before starting to use the overflow menu. Consider that using large items or small constraints, will reduce the effective maximum. Note: If the number of items supplied is bigger than max, at most max - 1 items will render, since the last one will be dedicated to the overflow composable. |
content | The content to be arranged in the row, defined using a dsl with [AppBarRowScope]. |
Code Example
SimpleTopAppBarWithAdaptiveActions
@OptIn(ExperimentalMaterial3Api::class)
@Preview
@Composable
fun SimpleTopAppBarWithAdaptiveActions() {
val sizeClass = currentWindowAdaptiveInfo().windowSizeClass
// Material guidelines state 3 items max in compact, and 5 items max elsewhere.
// To test this, try a resizable emulator, or a phone in landscape and portrait orientation.
val maxItemCount =
if (sizeClass.minWidthDp >= WindowSizeClass.WIDTH_DP_MEDIUM_LOWER_BOUND) {
5
} else {
3
}
Scaffold(
topBar = {
TopAppBar(
title = {
Text("Simple TopAppBar", maxLines = 1, overflow = TextOverflow.Ellipsis)
},
navigationIcon = {
IconButton(onClick = { /* doSomething() */ }) {
Icon(
imageVector = Icons.Filled.Menu,
contentDescription = "Localized description"
)
}
},
actions = {
AppBarRow(
maxItemCount = maxItemCount,
overflowIndicator = {
IconButton(onClick = { it.show() }) {
Icon(
imageVector = Icons.Filled.MoreVert,
contentDescription = "Localized description"
)
}
}
) {
clickableItem(
onClick = {},
icon = {
Icon(
imageVector = Icons.Filled.Attachment,
contentDescription = null
)
},
label = "Attachment"
)
clickableItem(
onClick = {},
icon = {
Icon(imageVector = Icons.Filled.Edit, contentDescription = null)
},
label = "Edit"
)
clickableItem(
onClick = {},
icon = {
Icon(imageVector = Icons.Outlined.Star, contentDescription = null)
},
label = "Favorite"
)
clickableItem(
onClick = {},
icon = {
Icon(imageVector = Icons.Filled.Snooze, contentDescription = null)
},
label = "Alarm"
)
clickableItem(
onClick = {},
icon = {
Icon(
imageVector = Icons.Outlined.MarkEmailUnread,
contentDescription = "Localized description"
)
},
label = "Email"
)
}
}
)
},
content = { innerPadding ->
LazyColumn(
contentPadding = innerPadding,
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
val list = (0..75).map { it.toString() }
items(count = list.size) {
Text(
text = list[it],
style = MaterialTheme.typography.bodyLarge,
modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp)
)
}
}
}
)
}