In Jetpack Compose Glimmer, a ToggleButton is an interactive component
that changes its appearance based on a checked state. Toggle buttons are
optimized for display glasses to provide clear visual transitions in shape and
color. These transitions indicate when an action or setting is active.
Use toggle buttons to expose actions that can be switched on or off. Unlike icon-only toggles, a toggle button primarily displays text content, though it supports optional icon slots for additional context.
For other use cases, there are also standard buttons and icon buttons.
Anatomy
A toggle button consists of a container that morphs between states and a label with optional icons.
| Part | Description |
|---|---|
Container |
Animates between a circular shape (unchecked) and a rounded rectangle (checked). |
Label |
Typically a |
Icons (optional) |
Leading or trailing slots that can vary based on state. |
Sizes
Like standard buttons, toggle buttons support two size variants:
| Size | Minimum height | Default usage |
|---|---|---|
Medium |
48.dp |
Default interactive size. |
Large |
72.dp |
Primary or high-emphasis toggles. |
Toggle button defaults
By default, toggle buttons use ToggleButtonDefaults.animateShape. This creates
a smooth transition between the following states:
- Unchecked:
GlimmerTheme.shapes.large(typically aCircleShape). - Checked:
ToggleButtonDefaults.CheckedShape(aRoundedCornerShapewith20.dpcorners).
The ToggleButtonColors class manages color resolution for the following
states:
- Unchecked: Defaults to
GlimmerTheme.colors.surface. - Checked: Defaults to
GlimmerTheme.colors.surface.
Animation
Toggle buttons use the following defaults for animation:
animateShape: Provides aShapethat interpolates corner sizes using a spring animation spec (stiffness = 600f).colors: A factory function to customize the background and content colors for both states.CheckedShape: A staticRoundedCornerShape(20.dp)used for the checked state.contentPadding: Inherits fromButtonDefaults.contentPadding.
Example: Toggle button
The following code creates a basic toggle button:
@Composable fun ToggleButtonSample() { var checked by remember { mutableStateOf(false) } val text = if (checked) "Toggle on" else "Toggle off" ToggleButton(checked = checked, onCheckedChange = { checked = it }) { Text(text) } }
Example: Toggle button with leading icon
The following code creates a toggle button with a leading icon:
@Composable fun ToggleButtonWithLeadingIconSample() { var checked by remember { mutableStateOf(false) } val text = if (checked) "Toggle on" else "Toggle off" ToggleButton( checked = checked, leadingIcon = { Icon(if (checked) FavoriteIcon else OutlinedFavoriteIcon, "Localized description") }, onCheckedChange = { checked = it }, ) { Text(text) } }