Build apps faster with our new App builder! Check it out →

IconToggleButton

Android

Component in Wear Material 3 Compose

Wear Material [IconToggleButton] is a filled icon toggle button which switches between primary colors and tonal colors depending on [checked] value, and offers a single slot for icon or image.

Set the size of the [IconToggleButton] with Modifier.[touchTargetAwareSize] to ensure that the background padding will correctly reach the edge of the minimum touch target. The recommended icon toggle button sizes are [IconToggleButtonDefaults.DefaultButtonSize], [IconToggleButtonDefaults.SmallButtonSize], [IconToggleButtonDefaults.LargeButtonSize] and [IconToggleButtonDefaults.ExtraLargeButtonSize].

Use [IconToggleButtonDefaults.iconSizeFor] to determine the icon size for a given [IconToggleButton] size, or refer to icon sizes, [IconToggleButtonDefaults.DefaultIconSize], [IconToggleButtonDefaults.LargeIconSize], [IconToggleButtonDefaults.ExtraLargeIconSize] directly.

[IconToggleButton] can be enabled or disabled. A disabled button will not respond to click events. When enabled, the checked and unchecked events are propagated by [onCheckedChange].

A simple icon toggle button using the default colors, animated when pressed.

Last updated:

Installation

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

Overloads

@Composable
fun IconToggleButton(
    checked: Boolean,
    onCheckedChange: (Boolean) -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    colors: IconToggleButtonColors = IconToggleButtonDefaults.iconToggleButtonColors(),
    interactionSource: MutableInteractionSource? = null,
    shapes: IconToggleButtonShapes = IconToggleButtonDefaults.shapes(),
    border: BorderStroke? = null,
    content: @Composable BoxScope.() -> Unit,
)

Parameters

namedescription
checkedBoolean flag indicating whether this toggle button is currently checked.
onCheckedChangeCallback to be invoked when this toggle button is clicked.
modifierModifier to be applied to the toggle button.
enabledControls the enabled state of the toggle button. When false, this toggle button will not be clickable.
colors[IconToggleButtonColors] that will be used to resolve the container and content color for this toggle button.
interactionSourcean optional hoisted [MutableInteractionSource] for observing and emitting [Interaction]s for this button. 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.
shapesDefines the shape for this toggle button. Defaults to a static shape based on [IconToggleButtonDefaults.shape], but animated versions are available through [IconToggleButtonDefaults.animatedShapes] and [IconToggleButtonDefaults.variantAnimatedShapes].
borderOptional [BorderStroke] for the [IconToggleButton].
contentThe content to be drawn inside the toggle button.

Code Examples

IconToggleButtonSample

@Composable
fun IconToggleButtonSample() {
    var checked by remember { mutableStateOf(true) }
    IconToggleButton(
        checked = checked,
        onCheckedChange = { checked = !checked },
        shapes = IconToggleButtonDefaults.animatedShapes(),
    ) {
        Icon(imageVector = Icons.Filled.Favorite, contentDescription = "Favorite icon")
    }
}

IconToggleButtonVariantSample

@Composable
fun IconToggleButtonVariantSample() {
    var checked by remember { mutableStateOf(true) }
    IconToggleButton(
        checked = checked,
        onCheckedChange = { checked = !checked },
        shapes = IconToggleButtonDefaults.variantAnimatedShapes(),
    ) {
        if (checked) {
            WifiOnIcon()
        } else {
            WifiOffIcon()
        }
    }
}
by @alexstyl