androidx.compose.material3

Switch

Composable function

A switch turns one setting on or off. The change takes effect right away.

The switch does not remember its state. You keep it in a variable and update it in onCheckedChange.

A switch that remembers its state
A switch that remembers its state
var on by remember { mutableStateOf(true) }
Switch(checked = on, onCheckedChange = { on = it })
On, off and disabled
On, off and disabled
Row(horizontalArrangement = Arrangement.spacedBy(16.dp)) {
    Switch(checked = true, onCheckedChange = {})
    Switch(checked = false, onCheckedChange = {})
    Switch(checked = true, onCheckedChange = {}, enabled = false)
    Switch(checked = false, onCheckedChange = {}, enabled = false)
}

Parameters

@Composable
fun Switch(
    checked: Boolean,
    onCheckedChange: ((Boolean) -> Unit)?,
    modifier: Modifier = Modifier,
    thumbContent: (@Composable () -> Unit)? = null,
    enabled: Boolean = true,
    colors: SwitchColors = SwitchDefaults.colors(),
    interactionSource: MutableInteractionSource? = null,
)

checked: BooleanRequired

Whether the switch is on. Pass your state here.

onCheckedChange: ((Boolean) -> Unit)?Required

Called with the new value when the user flips the switch. Update your state here. Pass null if the switch should not react, for example when a whole row handles the click.

modifier: ModifierOptional

Default: Modifier

Changes size, padding, or position of the switch. See the Modifier guide.

To make a label clickable too, put both in a Row and give the Row a Modifier.toggleable.

With a clickable label
With a clickable label
var wifi by remember { mutableStateOf(true) }
Row(
    modifier = Modifier.toggleable(value = wifi, onValueChange = { wifi = it }, role = Role.Switch),
    verticalAlignment = Alignment.CenterVertically,
) {
    Text("Wi-Fi", Modifier.width(200.dp))
    // The Row handles clicks, so the switch itself does not.
    Switch(checked = wifi, onCheckedChange = null)
}

thumbContent: (@Composable () -> Unit)?Optional

Default: null

Something drawn inside the thumb, usually an Icon. Size the icon with SwitchDefaults.IconSize.

An icon in the thumb
An icon in the thumb
var on by remember { mutableStateOf(true) }
Switch(
    checked = on,
    onCheckedChange = { on = it },
    thumbContent = if (on) {
        { Icon(Icons.Filled.Check, contentDescription = null, Modifier.size(SwitchDefaults.IconSize)) }
    } else null,
)

enabled: BooleanOptional

Default: true

Set to false to make the switch gray and not clickable.

colors: SwitchColorsOptional

Default: SwitchDefaults.colors()

Sets the thumb, track, border and icon colors for every state. Make one with SwitchDefaults.colors. Pass only the colors you want to change.

Tertiary colors when on
Tertiary colors when on
Switch(
    checked = true,
    onCheckedChange = {},
    colors = SwitchDefaults.colors(
        checkedThumbColor = MaterialTheme.colorScheme.onTertiary,
        checkedTrackColor = MaterialTheme.colorScheme.tertiary,
    ),
)

interactionSource: MutableInteractionSource?Optional

Default: null

Lets you observe or control press, hover and focus states. Leave it null unless your code must react to them. See InteractionSource.

See also