androidx.compose.material3

ButtonGroupSince 1.10

Composable function

A button group is a row of related buttons. There are two kinds:

A connected group to pick one optionExpressive theme
A connected group to pick one option
val options = listOf("Day", "Week", "Month")
var selected by remember { mutableIntStateOf(0) }
Row(horizontalArrangement = Arrangement.spacedBy(ButtonGroupDefaults.ConnectedSpaceBetween)) {
    options.forEachIndexed { index, label ->
        ToggleButton(
            checked = selected == index,
            onCheckedChange = { selected = index },
            shapes = when (index) {
                0 -> ButtonGroupDefaults.connectedLeadingButtonShapes()
                options.lastIndex -> ButtonGroupDefaults.connectedTrailingButtonShapes()
                else -> ButtonGroupDefaults.connectedMiddleButtonShapes()
            },
        ) { Text(label) }
    }
}

For a connected group, give each button the shape for its position from ButtonGroupDefaults: connectedLeadingButtonShapes() for the first, connectedMiddleButtonShapes() for the middle ones, and connectedTrailingButtonShapes() for the last. Use ButtonGroupDefaults.ConnectedSpaceBetween as the gap.

ButtonGroup takes items, not composables. Add them with clickableItem and toggleableItem. Items that don't fit move into an overflow menu.

A group with overflow menuExpressive theme
A group with overflow menu
var bold by remember { mutableStateOf(true) }
var italic by remember { mutableStateOf(false) }
ButtonGroup(
    overflowIndicator = { menuState -> ButtonGroupDefaults.OverflowIndicator(menuState) },
) {
    toggleableItem(checked = bold, onCheckedChange = { bold = it }, label = "Bold")
    toggleableItem(checked = italic, onCheckedChange = { italic = it }, label = "Italic")
    clickableItem(onClick = {}, label = "Clear")
    clickableItem(onClick = {}, label = "Font")
    clickableItem(onClick = {}, label = "Size")
}

Parameters

@Composable
fun ButtonGroup(
    overflowIndicator: @Composable (ButtonGroupMenuState) -> Unit,
    modifier: Modifier = Modifier,
    expandedRatio: Float = ButtonGroupDefaults.ExpandedRatio,
    horizontalArrangement: Arrangement.Horizontal = ButtonGroupDefaults.HorizontalArrangement,
    verticalAlignment: Alignment.Vertical = Alignment.Top,
    content: ButtonGroupScope.() -> Unit,
)

overflowIndicator: @Composable (ButtonGroupMenuState) -> UnitRequired

The button that opens the menu of items that don't fit. Use ButtonGroupDefaults.OverflowIndicator(menuState).

modifier: ModifierOptional

Default: Modifier

Changes size, padding, or position. The width decides how many items fit.

expandedRatio: FloatOptional

Default: ButtonGroupDefaults.ExpandedRatio

How much a pressed button grows, as a part of its width. Default: 0.15 (15 %).

horizontalArrangement: Arrangement.HorizontalOptional

Default: ButtonGroupDefaults.HorizontalArrangement

Space between the buttons.

verticalAlignment: Alignment.VerticalOptionalSince 1.11

Default: Alignment.Top

How buttons of different heights line up.

content: ButtonGroupScope.() -> UnitRequired

The items. Add them with clickableItem(onClick, label), toggleableItem(checked, label, onCheckedChange), or customItem for anything else.

See also