Compose Component

Switch

Material Design switch Switches toggle the state of a single item on or off.

Switch social preview

SwitchSample

@Preview
@Sampled
@Composable
fun SwitchSample() {
    var checked by rememberSaveable { mutableStateOf(true) }
    Switch(
        modifier = Modifier.semantics { contentDescription = "Demo" },
        checked = checked,
        onCheckedChange = { checked = it },
    )
}

SwitchWithThumbIconSample

@Preview
@Sampled
@Composable
fun SwitchWithThumbIconSample() {
    var checked by rememberSaveable { mutableStateOf(true) }

    Switch(
        modifier = Modifier.semantics { contentDescription = "Demo with icon" },
        checked = checked,
        onCheckedChange = { checked = it },
        thumbContent = {
            if (checked) {
                // Icon isn't focusable, no need for content description
                Icon(
                    imageVector = Icons.Filled.Check,
                    contentDescription = null,
                    modifier = Modifier.size(SwitchDefaults.IconSize),
                )
            }
        },
    )
}
Jetpack Compose beyond MaterialFree components, screens and templates for apps that don’t look like Google’s. Copy them into your app, or let your coding agent add them.Browse for free

Content updated: