We just launched Compose Examples featuring over 150+ components! Check it out →

Switch

Android

Component in Jetpack Glance

Adds a switch view to the glance view.

Last updated:

Installation

dependencies {
   implementation("androidx.glance:glance-appwidget:1.1.1")
}

Overloads

@Composable
fun Switch(
    checked: Boolean,
    onCheckedChange: Action?,
    modifier: GlanceModifier = GlanceModifier,
    text: String = "",
    style: TextStyle? = null,
    colors: SwitchColors = SwitchDefaults.colors(),
    maxLines: Int = Int.MAX_VALUE,
)

Parameters

namedescription
checkedwhether the switch is checked
onCheckedChangethe action to be run when the switch is clicked. The current value ofchecked is provided to this action in its ActionParameters, and can be retrieved using the[ToggleableStateKey]. If this action launches an activity, the current value of checked will bepassed as an intent extra with the name [RemoteViews.EXTRA_CHECKED].In order to allow the Launcher to provide this extra on Android version S and later, we use amutable PendingIntent ([android.app.PendingIntent.FLAG_MUTABLE]) when this action is not alambda. Before S, and for lambda actions, this will be an immutable PendingIntent.
modifierthe modifier to apply to the switch
textthe text to display to the end of the switch
stylethe style to apply to [text]
colorsthe tint colors for the thumb and track of the switch
maxLinesAn optional maximum number of lines for the text to span, wrapping ifnecessary. If the text exceeds the given number of lines, it will be truncated.
@Composable
fun Switch(
    checked: Boolean,
    onCheckedChange: () -> Unit,
    modifier: GlanceModifier = GlanceModifier,
    text: String = "",
    style: TextStyle? = null,
    colors: SwitchColors = SwitchDefaults.colors(),
    maxLines: Int = Int.MAX_VALUE,
)

Parameters

namedescription
checkedwhether the switch is checked
onCheckedChangethe action to be run when the switch is clicked
modifierthe modifier to apply to the switch
textthe text to display to the end of the switch
stylethe style to apply to [text]
colorsthe tint colors for the thumb and track of the switch
maxLinesAn optional maximum number of lines for the text to span, wrapping ifnecessary. If the text exceeds the given number of lines, it will be truncated.
@ExperimentalGlanceApi
@Composable
fun Switch(
    checked: Boolean,
    onCheckedChange: () -> Unit,
    modifier: GlanceModifier = GlanceModifier,
    text: String = "",
    style: TextStyle? = null,
    colors: SwitchColors = SwitchDefaults.colors(),
    maxLines: Int = Int.MAX_VALUE,
    key: String? = null,
)

Parameters

namedescription
checkedwhether the switch is checked
onCheckedChangethe action to be run when the switch is clicked
modifierthe modifier to apply to the switch
textthe text to display to the end of the switch
stylethe style to apply to [text]
colorsthe tint colors for the thumb and track of the switch
maxLinesAn optional maximum number of lines for the text to span, wrapping ifnecessary. If the text exceeds the given number of lines, it will be truncated.
keyA stable and unique key that identifies the action for this switch. This ensuresthat the correct action is triggered, especially in cases of items that change order. If notprovided we use the key that is automatically generated by the Compose runtime, which is uniquefor every exact code location in the composition tree.
by @alexstyl