Checkbox

Lets people select one or more items from a set, or turn an option on or off.

When to use

Use checkboxes for independent choices. A parent checkbox with an indeterminate state can summarise a group of children.

Make the label part of the touch target by putting the checkbox and the text in one toggleable row, and pass null as onCheckedChange to the checkbox itself.

Playground

The interactive page has a playground with live property controls for this component and generated code.

Examples

Parent with indeterminate state

val children = remember { mutableStateOf(listOf(true, false, false)) }
val parentState = when {
    children.value.all { it } -> ToggleableState.On
    children.value.none { it } -> ToggleableState.Off
    else -> ToggleableState.Indeterminate
}

Column {
    Row(Modifier.clickable { children.value = List(3) { parentState != ToggleableState.On } }, verticalAlignment = Alignment.CenterVertically) {
        TriStateCheckbox(state = parentState, onClick = null)
        Text("Select all")
    }
    children.value.forEachIndexed { index, value ->
        Row(
            Modifier.padding(start = 24.dp).toggleable(value, role = Role.Checkbox) { checked ->
                children.value = children.value.toMutableList().also { it[index] = checked }
            },
            verticalAlignment = Alignment.CenterVertically,
        ) {
            Checkbox(checked = value, onCheckedChange = null)
            Text("Option ${index + 1}")
        }
    }
}

Parameters

NameTypeDefaultDescription
checkedBooleanWhether the box is checked.
onCheckedChange((Boolean) -> Unit)?Pass null when a parent handles the toggle, for example Modifier.toggleable.
enabledBooleantrueDisables interaction.
colorsCheckboxColorsCheckboxDefaults.colors()Box, check mark and border colors.

Guidelines

Do:

Don't:

Accessibility

Related components