Skip to content

Switch - Usage ​

The Switch component allows users to toggle between two states.

AndroidreleasedElementsreleasediOSreleasedReactreleasedVuereleased

Android ​

Syntax ​

kotlin
fun WarpSwitch(
    modifier: Modifier = Modifier,
    checked: Boolean = false,
    onCheckedChange: ((Boolean) -> Unit) = { },
    enabled: Boolean = true,
    contentDescr: String? = null
)

The switch has only two appearances - default or disabled

kotlin
var checked by remember { mutableStateOf(true) }
WarpSwitch(
    checked = checked,
    onCheckedChange = { checked = it }
    )

To display a disabled switch - set enabled to false

kotlin
var checked by remember { mutableStateOf(true) }

WarpSwitch(
    checked = checked,
    onCheckedChange = { checked = it },
    enabled = false
    )

Legacy support ​

To support layouts still written in xml the WarpSwitch can be used as a custom view. To add functionality, set onCheckedChanged listener.

kotlin
fun setOnCheckedChangeListener(onCheckedChangeListener: ((Boolean) -> Unit))
xml
<com.schibsted.nmp.warp.components.legacy.WarpSwitchView
    android:id="@+id/warp_switch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/space2"
    app:switchChecked="true"
    app:switchEnabled="true"
     />

Parameters ​

Optional Props ​

NameTypeDefaultDescription
modifierModifierModifierSets the modifier for the switch
onCheckedChange(Boolean) -> UnitLambda to be invoked when checked or unchecked
enabledbooleantrueWhether the switch is enabled or not
checkedbooleanfalseWhether the switch is on or off
contentDescrStringnullThe a11y text for screenreaders

Questions? ​

Feel free to ask any questions on usage in the Warp DS Slack channel: #warp-design-system