androidx.compose.material3

ToggleButtonSince 1.10

Composable function

A toggle button is a Button that stays on or off, like "Bold" in a text editor. When checked, it changes color and shape.

The button does not remember its state. You keep it in a variable and update it in onCheckedChange.

Filled, elevated, tonal and outlined; the first one checkedExpressive theme
Filled, elevated, tonal and outlined; the first one checked
val checked = remember { mutableStateListOf(true, false, false, false) }
Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
    ToggleButton(checked = checked[0], onCheckedChange = { checked[0] = it }) { Text("Filled") }
    ElevatedToggleButton(checked = checked[1], onCheckedChange = { checked[1] = it }) { Text("Elevated") }
    TonalToggleButton(checked = checked[2], onCheckedChange = { checked[2] = it }) { Text("Tonal") }
    OutlinedToggleButton(checked = checked[3], onCheckedChange = { checked[3] = it }) { Text("Outlined") }
}

Toggle buttons are the pieces of a connected ButtonGroup.

Parameters

@Composable
fun ToggleButton(
    checked: Boolean,
    onCheckedChange: (Boolean) -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    shapes: ToggleButtonShapes = ToggleButtonDefaults.shapesFor(ButtonDefaults.MinHeight),
    colors: ToggleButtonColors = ToggleButtonDefaults.toggleButtonColors(),
    elevation: ButtonElevation? = ButtonDefaults.buttonElevation(),
    border: BorderStroke? = null,
    contentPadding: PaddingValues = ButtonDefaults.contentPaddingFor(ButtonDefaults.MinHeight),
    interactionSource: MutableInteractionSource? = null,
    content: @Composable RowScope.() -> Unit,
)

checked: BooleanRequired

Whether the button is on. Pass your state here.

onCheckedChange: (Boolean) -> UnitRequired

Called with the new value when the button is clicked. Update your state here.

modifier: ModifierOptional

Default: Modifier

Changes size, padding, or position. See the Modifier guide.

enabled: BooleanOptional

Default: true

Set to false to make the button gray and not clickable.

shapes: ToggleButtonShapesOptional

Default: ToggleButtonDefaults.shapesFor(ButtonDefaults.MinHeight)

Sets three shapes: at rest, pressed and checked. Make one with ToggleButtonDefaults.shapes. Default: round at rest, square when checked.

colors: ToggleButtonColorsOptional

Default: ToggleButtonDefaults.toggleButtonColors()

Sets colors for on, off and disabled. Make one with ToggleButtonDefaults.toggleButtonColors. Each type has its own function.

elevation: ButtonElevation?Optional

Default: ButtonDefaults.buttonElevation()

Sets the shadow height. Pass null for no shadow.

border: BorderStroke?Optional

Default: null

Draws an outline. OutlinedToggleButton has one by default.

contentPadding: PaddingValuesOptional

Default: ButtonDefaults.contentPaddingFor(ButtonDefaults.MinHeight)

Space between the edge of the button and the content.

interactionSource: MutableInteractionSource?Optional

Default: null

Lets you observe or control press, hover and focus states. Leave it null unless your code must react to them. See InteractionSource.

content: @Composable RowScope.() -> UnitRequired

What the button shows. Usually a Text, sometimes an Icon and a Text. Placed in a Row.

With an iconExpressive theme
With an icon
var starred by remember { mutableStateOf(true) }
ToggleButton(checked = starred, onCheckedChange = { starred = it }) {
    Icon(Icons.Filled.Star, contentDescription = null, Modifier.size(ToggleButtonDefaults.IconSize))
    Spacer(Modifier.size(ToggleButtonDefaults.IconSpacing))
    Text(if (starred) "Starred" else "Star")
}

Toggle button types

See also