Toggle Button in Jetpack Compose: IconToggleButton, ToggleButton & Switch

A toggle button switches between two states – on/off, liked/not liked, bold/normal. In Jetpack Compose (Material 3) you have three good options: an icon toggle button for icon-only actions like a heart or bookmark, a toggle button with text, and a Switch for settings. This guide shows each one with copy-paste Kotlin code.

Icon Toggle Button in Jetpack Compose (UnLike)

Which toggle should you use?

ComponentUse it forLooks like
IconToggleButton (+ Filled, Tonal, Outlined)Icon-only actions: favorite, bookmark, muteAn icon that changes when checked
ToggleButton (+ Elevated, Tonal, Outlined)A labelled on/off action: “Follow”, “Bold”A button that stays pressed when checked
SwitchSettings that apply right away: dark mode, notificationsA sliding switch
SingleChoiceSegmentedButtonRowChoosing one option from 2–5Connected buttons

1. IconToggleButton (heart / favorite)

IconToggleButton takes a checked value and an onCheckedChange callback. Keep the state in a rememberSaveable variable so it survives screen rotation.

@Composable
fun FavoriteToggle() {
    var checked by rememberSaveable { mutableStateOf(false) }

    IconToggleButton(
        checked = checked,
        onCheckedChange = { checked = it }
    ) {
        Icon(
            imageVector = if (checked) Icons.Filled.Favorite else Icons.Outlined.FavoriteBorder,
            contentDescription = if (checked) "Remove from favorites" else "Add to favorites",
            tint = if (checked) Color(0xFFE53935) else LocalContentColor.current
        )
    }
}

Changing the contentDescription with the state tells TalkBack users what the button will do.

Filled, tonal and outlined variants

For more emphasis, swap in FilledIconToggleButton, FilledTonalIconToggleButton or OutlinedIconToggleButton – same parameters:

FilledIconToggleButton(checked = checked, onCheckedChange = { checked = it }) {
    Icon(Icons.Filled.Lock, contentDescription = "Lock")
}

OutlinedIconToggleButton(checked = checked, onCheckedChange = { checked = it }) {
    Icon(Icons.Filled.Notifications, contentDescription = "Notifications")
}

Custom colors

IconToggleButton(
    checked = checked,
    onCheckedChange = { checked = it },
    colors = IconButtonDefaults.iconToggleButtonColors(
        contentColor = Color.Gray,
        checkedContentColor = Color(0xFF3D63E6)
    )
) {
    Icon(Icons.Filled.Bookmark, contentDescription = "Bookmark")
}

2. ToggleButton with text

Newer Material 3 versions add text toggle buttons in four styles – ToggleButton, ElevatedToggleButton, TonalToggleButton and OutlinedToggleButton (Android Developers). They are part of Material 3 Expressive, so depending on your Compose version you may need an opt-in:

@OptIn(ExperimentalMaterial3ExpressiveApi::class)
@Composable
fun FollowToggle() {
    var following by rememberSaveable { mutableStateOf(false) }

    ToggleButton(
        checked = following,
        onCheckedChange = { following = it }
    ) {
        Icon(
            if (following) Icons.Filled.Check else Icons.Filled.Add,
            contentDescription = null,
            modifier = Modifier.size(ButtonDefaults.IconSize)
        )
        Spacer(Modifier.size(ButtonDefaults.IconSpacing))
        Text(if (following) "Following" else "Follow")
    }
}

On older Compose versions without ToggleButton, use a normal Button and change its colors and label based on the state.

3. Switch for settings

@Composable
fun DarkModeSetting() {
    var enabled by rememberSaveable { mutableStateOf(false) }

    Row(
        verticalAlignment = Alignment.CenterVertically,
        modifier = Modifier
            .fillMaxWidth()
            .toggleable(value = enabled, role = Role.Switch, onValueChange = { enabled = it })
            .padding(16.dp)
    ) {
        Text("Dark mode", modifier = Modifier.weight(1f))
        Switch(checked = enabled, onCheckedChange = null)
    }
}

Putting toggleable on the whole row makes the label clickable too and reads correctly in TalkBack; that’s why the Switch itself has onCheckedChange = null.

4. Pick one option: segmented buttons

@Composable
fun SizePicker() {
    val options = listOf("S", "M", "L")
    var selected by rememberSaveable { mutableIntStateOf(1) }

    SingleChoiceSegmentedButtonRow {
        options.forEachIndexed { index, label ->
            SegmentedButton(
                selected = index == selected,
                onClick = { selected = index },
                shape = SegmentedButtonDefaults.itemShape(index, options.size)
            ) { Text(label) }
        }
    }
}

Common problems

ProblemFix
The toggle resets when the screen rotatesUse rememberSaveable instead of remember.
Icons.Outlined.FavoriteBorder not foundAdd the androidx.compose.material:material-icons-extended dependency, or use an icon from your own drawables.
ToggleButton is unresolvedUpdate Compose Material 3, or use IconToggleButton / a styled Button.
State should come from a ViewModelPass checked and onCheckedChange as parameters (state hoisting) instead of keeping state inside the composable.

FAQ

What is the difference between IconButton and IconToggleButton?

IconButton runs an action on click. IconToggleButton keeps a checked state and tells you when it changes.

How do I make a like button in Jetpack Compose?

Use IconToggleButton with a filled heart when checked and an outlined heart when not, as in the first example.

More Jetpack Compose guides

WhatsApp us📞 Call now