New Compose Multiplatform components arrived on Composables UICheck it out →

Component in Wear Material 3 Compose

TextToggleButton

Android

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

Set the size of the [TextToggleButton] with Modifier.[touchTargetAwareSize] to ensure that the background padding will correctly reach the edge of the minimum touch target. The recommended [TextToggleButton] sizes are [TextButtonDefaults.DefaultButtonSize], [TextButtonDefaults.LargeButtonSize] and [TextButtonDefaults.SmallButtonSize]. [TextToggleButton] uses [Typography.labelMedium] by default and this should be overridden to [Typography.labelLarge] when using [TextButtonDefaults.LargeButtonSize].

[TextToggleButton] 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].

Last updated:

Installation

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

Overloads

@Composable
fun TextToggleButton(
    checked: Boolean,
    onCheckedChange: (Boolean) -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    colors: ToggleButtonColors = TextButtonDefaults.textToggleButtonColors(),
    interactionSource: MutableInteractionSource? = null,
    shape: Shape = TextButtonDefaults.shape,
    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[ToggleButtonColors] 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 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.
shapeDefines the shape for this toggle button. It is strongly recommended to use the default as this shape is a key characteristic of the Wear Material 3 Theme.
borderOptional [BorderStroke] for the [TextToggleButton].
contentThe text to be drawn inside the toggle button.

Code Examples

TextToggleButtonSample

@Composable
@Sampled
fun TextToggleButtonSample() {
    var checked by remember { mutableStateOf(true) }
    TextToggleButton(
        checked = checked,
        onCheckedChange = { checked = !checked }
    ) {
        Text(
            text = if (checked) "On" else "Off"
        )
    }
}

LargeTextToggleButtonSample

@Composable
@Sampled
fun LargeTextToggleButtonSample() {
    var checked by remember { mutableStateOf(true) }
    TextToggleButton(
        checked = checked,
        onCheckedChange = { checked = !checked },
        modifier = Modifier.touchTargetAwareSize(TextButtonDefaults.LargeButtonSize),
    ) {
        Text(
            text = if (checked) "On" else "Off",
            style = MaterialTheme.typography.labelLarge,
        )
    }
}