{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

ToggleableKt

public final class ToggleableKt


Summary

Public methods

static final @NonNull Modifier
toggleable(
    @NonNull Modifier receiver,
    boolean value,
    boolean enabled,
    Role role,
    @NonNull Function1<@NonNull BooleanUnit> onValueChange
)

Configure component to make it toggleable via input and accessibility events

static final @NonNull Modifier
toggleable(
    @NonNull Modifier receiver,
    boolean value,
    @NonNull MutableInteractionSource interactionSource,
    Indication indication,
    boolean enabled,
    Role role,
    @NonNull Function1<@NonNull BooleanUnit> onValueChange
)

Configure component to make it toggleable via input and accessibility events.

static final @NonNull Modifier
triStateToggleable(
    @NonNull Modifier receiver,
    @NonNull ToggleableState state,
    boolean enabled,
    Role role,
    @NonNull Function0<Unit> onClick
)

Configure component to make it toggleable via input and accessibility events with three states: On, Off and Indeterminate.

static final @NonNull Modifier
triStateToggleable(
    @NonNull Modifier receiver,
    @NonNull ToggleableState state,
    @NonNull MutableInteractionSource interactionSource,
    Indication indication,
    boolean enabled,
    Role role,
    @NonNull Function0<Unit> onClick
)

Configure component to make it toggleable via input and accessibility events with three states: On, Off and Indeterminate.

Public methods

toggleable

public static final @NonNull Modifier toggleable(
    @NonNull Modifier receiver,
    boolean value,
    boolean enabled,
    Role role,
    @NonNull Function1<@NonNull BooleanUnit> onValueChange
)

Configure component to make it toggleable via input and accessibility events

This version has no MutableInteractionSource or Indication parameters, default indication from LocalIndication will be used. To specify MutableInteractionSource or Indication, use another overload.

import androidx.compose.foundation.selection.toggleable
import androidx.compose.material.Text
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier

var checked by remember { mutableStateOf(false) }
// content that you want to make toggleable
Text(
    modifier = Modifier.toggleable(value = checked, onValueChange = { checked = it }),
    text = checked.toString()
)
Parameters
boolean value

whether Toggleable is on or off

boolean enabled

whether or not this toggleable will handle input events and appear enabled for semantics purposes

Role role

the type of user interface element. Accessibility services might use this to describe the element or do customizations

@NonNull Function1<@NonNull BooleanUnit> onValueChange

callback to be invoked when toggleable is clicked, therefore the change of the state in requested.

See also
triStateToggleable

if you require support for an indeterminate state.

toggleable

public static final @NonNull Modifier toggleable(
    @NonNull Modifier receiver,
    boolean value,
    @NonNull MutableInteractionSource interactionSource,
    Indication indication,
    boolean enabled,
    Role role,
    @NonNull Function1<@NonNull BooleanUnit> onValueChange
)

Configure component to make it toggleable via input and accessibility events.

This version requires both MutableInteractionSource and Indication to work properly. Use another overload if you don't need these parameters.

import androidx.compose.foundation.selection.toggleable
import androidx.compose.material.Text
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier

var checked by remember { mutableStateOf(false) }
// content that you want to make toggleable
Text(
    modifier = Modifier.toggleable(value = checked, onValueChange = { checked = it }),
    text = checked.toString()
)
Parameters
boolean value

whether Toggleable is on or off

@NonNull MutableInteractionSource interactionSource

MutableInteractionSource that will be used to emit PressInteraction.Press when this toggleable is being pressed.

Indication indication

indication to be shown when modified element is pressed. Be default, indication from LocalIndication will be used. Pass null to show no indication, or current value from LocalIndication to show theme default

boolean enabled

whether or not this toggleable will handle input events and appear enabled for semantics purposes

Role role

the type of user interface element. Accessibility services might use this to describe the element or do customizations

@NonNull Function1<@NonNull BooleanUnit> onValueChange

callback to be invoked when toggleable is clicked, therefore the change of the state in requested.

See also
triStateToggleable

if you require support for an indeterminate state.

triStateToggleable

public static final @NonNull Modifier triStateToggleable(
    @NonNull Modifier receiver,
    @NonNull ToggleableState state,
    boolean enabled,
    Role role,
    @NonNull Function0<Unit> onClick
)

Configure component to make it toggleable via input and accessibility events with three states: On, Off and Indeterminate.

TriStateToggleable should be used when there are dependent Toggleables associated to this component and those can have different values.

This version has no MutableInteractionSource or Indication parameters, default indication from LocalIndication will be used. To specify MutableInteractionSource or Indication, use another overload.

import androidx.compose.foundation.selection.toggleable
import androidx.compose.foundation.selection.triStateToggleable
import androidx.compose.material.Text
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.state.ToggleableState

var checked by remember { mutableStateOf(ToggleableState.Indeterminate) }
// content that you want to make toggleable
Text(
    modifier = Modifier.triStateToggleable(
        state = checked,
        onClick = {
            checked =
                if (checked == ToggleableState.On) ToggleableState.Off else ToggleableState.On
        }
    ),
    text = checked.toString()
)
Parameters
@NonNull ToggleableState state

current value for the component

boolean enabled

whether or not this triStateToggleable will handle input events and appear enabled for semantics purposes

Role role

the type of user interface element. Accessibility services might use this to describe the element or do customizations

@NonNull Function0<Unit> onClick

will be called when user clicks the toggleable.

See also
toggleable

if you want to support only two states: on and off

triStateToggleable

public static final @NonNull Modifier triStateToggleable(
    @NonNull Modifier receiver,
    @NonNull ToggleableState state,
    @NonNull MutableInteractionSource interactionSource,
    Indication indication,
    boolean enabled,
    Role role,
    @NonNull Function0<Unit> onClick
)

Configure component to make it toggleable via input and accessibility events with three states: On, Off and Indeterminate.

TriStateToggleable should be used when there are dependent Toggleables associated to this component and those can have different values.

This version requires both MutableInteractionSource and Indication to work properly. Use another overload if you don't need these parameters.

import androidx.compose.foundation.selection.toggleable
import androidx.compose.foundation.selection.triStateToggleable
import androidx.compose.material.Text
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.state.ToggleableState

var checked by remember { mutableStateOf(ToggleableState.Indeterminate) }
// content that you want to make toggleable
Text(
    modifier = Modifier.triStateToggleable(
        state = checked,
        onClick = {
            checked =
                if (checked == ToggleableState.On) ToggleableState.Off else ToggleableState.On
        }
    ),
    text = checked.toString()
)
Parameters
@NonNull ToggleableState state

current value for the component

@NonNull MutableInteractionSource interactionSource

MutableInteractionSource that will be used to emit PressInteraction.Press when this triStateToggleable is being pressed.

Indication indication

indication to be shown when modified element is pressed. Be default, indication from LocalIndication will be used. Pass null to show no indication, or current value from LocalIndication to show theme default

boolean enabled

whether or not this triStateToggleable will handle input events and appear enabled for semantics purposes

Role role

the type of user interface element. Accessibility services might use this to describe the element or do customizations

@NonNull Function0<Unit> onClick

will be called when user clicks the toggleable.

See also
toggleable

if you want to support only two states: on and off