We just launched Compose Examples featuring over 150+ components! Check it out →

ToggleButton

Common

Component in Material 3 Compose

Toggle button is a toggleable button that switches between primary and tonal colors depending on [checked]'s value. It also morphs between the three shapes provided in [shapes] depending on the state of the interaction with the toggle button as long as the three shapes provided our [CornerBasedShape]s. If a shape in [shapes] isn't a [CornerBasedShape], then toggle button will toggle between the [ButtonShapes] according to user interaction.

see [Button] for a static button that doesn't need to be toggled. see [IconToggleButton] for a toggleable button where the content is specifically an [Icon].

Last updated:

Installation

dependencies {
   implementation("androidx.compose.material3:material3:1.4.0-alpha01")
}

Overloads

@Composable
@ExperimentalMaterial3ExpressiveApi
fun ToggleButton(
    checked: Boolean,
    onCheckedChange: (Boolean) -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    shapes: ButtonShapes =
        ToggleButtonDefaults.shapes(
            ToggleButtonDefaults.shape,
            ToggleButtonDefaults.pressedShape,
            ToggleButtonDefaults.checkedShape
        ),
    colors: ToggleButtonColors = ToggleButtonDefaults.toggleButtonColors(),
    elevation: ButtonElevation? = ButtonDefaults.buttonElevation(),
    border: BorderStroke? = null,
    contentPadding: PaddingValues = ToggleButtonDefaults.ContentPadding,
    interactionSource: MutableInteractionSource? = null,
    content: @Composable RowScope.() -> Unit
)

Parameters

namedescription
checkedwhether the toggle button is toggled on or off.
onCheckedChangecalled when the toggle button is clicked.
modifierthe [Modifier] to be applied to the toggle button.
enabledcontrols the enabled state of this toggle button. When false, this component will not respond to user input, and it will appear visually disabled and disabled to accessibility services.
shapesthe [ButtonShapes] that the toggle button will morph between depending on the user's interaction with the toggle button.
colors[ToggleButtonColors] that will be used to resolve the colors used for this toggle button in different states. See [ToggleButtonDefaults.toggleButtonColors].
elevation[ButtonElevation] used to resolve the elevation for this button in different states. This controls the size of the shadow below the button. See [ButtonElevation.shadowElevation]. Additionally, when the container color is [ColorScheme.surface], this controls the amount of primary color applied as an overlay.
borderthe border to draw around the container of this toggle button.
contentPaddingthe spacing values to apply internally between the container and the content
interactionSourcean optional hoisted [MutableInteractionSource] for observing and emitting [Interaction]s for this toggle button. You can use this to change the toggle button's appearance or preview the toggle button in different states. Note that if null is provided, interactions will still happen internally.
contentThe content displayed on the toggle button, expected to be text, icon or image.

Code Examples

ToggleButtonSample

@OptIn(ExperimentalMaterial3ExpressiveApi::class)
@Preview
@Composable
fun ToggleButtonSample() {
    var checked by remember { mutableStateOf(false) }
    ToggleButton(checked = checked, onCheckedChange = { checked = it }) { Text("Button") }
}

ToggleButtonWithIconSample

@OptIn(ExperimentalMaterial3ExpressiveApi::class)
@Preview
@Composable
fun ToggleButtonWithIconSample() {
    var checked by remember { mutableStateOf(false) }
    ElevatedToggleButton(checked = checked, onCheckedChange = { checked = it }) {
        Icon(
            if (checked) Icons.Filled.Favorite else Icons.Outlined.Favorite,
            contentDescription = "Localized description",
            modifier = Modifier.size(ToggleButtonDefaults.IconSize)
        )
        Spacer(Modifier.size(ToggleButtonDefaults.IconSpacing))
        Text("Like")
    }
}

RoundToggleButtonSample

@OptIn(ExperimentalMaterial3ExpressiveApi::class)
@Preview
@Composable
fun RoundToggleButtonSample() {
    var checked by remember { mutableStateOf(false) }
    val shapes =
        ButtonShapes(
            shape = ToggleButtonDefaults.roundShape,
            pressedShape = ToggleButtonDefaults.pressedShape,
            checkedShape = ToggleButtonDefaults.squareShape
        )
    ToggleButton(checked = checked, onCheckedChange = { checked = it }, shapes = shapes) {
        Text("Button")
    }
}
by @alexstyl