New Compose Multiplatform components arrived on Composables UICheck it out →

Component in Wear Material 3 Compose

ToggleButton

Android

The Wear Material [ToggleButton] offers four slots and a specific layout for an icon, a label, a secondaryLabel and toggle control (such as [Checkbox] or [Switch]). The icon and secondaryLabel are optional. The items are laid out in a row with the optional icon at the start, a column containing the two label slots in the middle and a slot for the toggle control at the end.

The [ToggleButton] is Stadium shaped and has a max height designed to take no more than two lines of text. With localisation and/or large font sizes, the [ToggleButton] height adjusts to accommodate the contents. The label and secondary label should be start aligned.

Last updated:

Installation

dependencies {
   implementation("androidx.wear.compose:compose-material3:1.0.0-alpha23")
}

Overloads

@Composable
fun ToggleButton(
    checked: Boolean,
    onCheckedChange: (Boolean) -> Unit,
    toggleControl: @Composable ToggleControlScope.() -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    shape: Shape = ToggleButtonTokens.ContainerShape.value,
    colors: ToggleButtonColors = ToggleButtonDefaults.toggleButtonColors(),
    contentPadding: PaddingValues = ToggleButtonDefaults.ContentPadding,
    interactionSource: MutableInteractionSource? = null,
    icon: @Composable (BoxScope.() -> Unit)? = null,
    secondaryLabel: @Composable (RowScope.() -> Unit)? = null,
    label: @Composable RowScope.() -> Unit
)

Parameters

namedescription
checkedBoolean flag indicating whether this button is currently checked.
onCheckedChangeCallback to be invoked when this buttons checked status is changed.
toggleControlA slot for providing the button's toggle control. Two built-in types of toggle control are supported: [Checkbox] and [Switch].
modifierModifier to be applied to the [ToggleButton].
enabledControls the enabled state of the button. When false, this button will not be clickable.
shapeDefines the button's shape. It is strongly recommended to use the default as this shape is a key characteristic of the Wear Material Theme.
colors[ToggleButtonColors] that will be used to resolve the background and content color for this button in different states.
contentPaddingThe spacing values to apply internally between the container and the content.
interactionSourcean optional hoisted [MutableInteractionSource] for observing and emitting [Interaction]s for this button's "toggleable" tap area. You can use this to change the button's appearance or preview the button in different states. Note that if null is provided, interactions will still happen internally.
iconAn optional slot for providing an icon to indicate the purpose of the button. The contents are expected to be a horizontally and vertically center aligned icon of size 24.dp.
secondaryLabelA slot for providing the button's secondary label. The contents are expected to be text which is "start" aligned.
labelA slot for providing the button's main label. The contents are expected to be text which is "start" aligned.

Code Examples

ToggleButtonWithCheckbox

@Composable
@Sampled
fun ToggleButtonWithCheckbox() {
    var checked by remember { mutableStateOf(true) }
    ToggleButton(
        label = {
            Text("Checkbox", maxLines = 3, overflow = TextOverflow.Ellipsis)
        },
        secondaryLabel = {
            Text("With secondary label", maxLines = 2, overflow = TextOverflow.Ellipsis)
        },
        checked = checked,
        toggleControl = { Checkbox() },
        onCheckedChange = { checked = it },
        icon = {
            Icon(
                Icons.Filled.Favorite,
                contentDescription = "Favorite icon"
            )
        },
        enabled = true,
    )
}

ToggleButtonWithSwitch

@Composable
@Sampled
fun ToggleButtonWithSwitch() {
    var checked by remember { mutableStateOf(true) }
    ToggleButton(
        label = {
            Text("Switch", maxLines = 3, overflow = TextOverflow.Ellipsis)
        },
        secondaryLabel = {
            Text("With secondary label", maxLines = 2, overflow = TextOverflow.Ellipsis)
        },
        checked = checked,
        toggleControl = { Switch() },
        onCheckedChange = { checked = it },
        icon = {
            Icon(
                Icons.Filled.Favorite,
                contentDescription = "Favorite icon"
            )
        },
        enabled = true,
    )
}