UX4G Logo
Components

Toggle Switch

Accessible binary on/off switches with smooth animations, customizable sizes, label positions, required markers, and trailing icons

The UX4G Toggle Switch component provides an accessible binary on/off control for user preferences, SMS alerts, biometric authentication, and conditional form inputs in government portals.

Key Capabilities

  • Standalone & Labeled Variants: Use as a standalone switch (Ux4gSwitchLabelPosition.NoLabel) or alongside descriptive labels with helper descriptions.
  • Flexible Label Positioning: Place labels on the Right (default), Left, or on BothSides (e.g., Monthly vs Annual billing switch).
  • Required Asterisk & Trailing Icons: Mark critical consent switches with isRequired = true and attach informative trailing icons (trailingIcon).
  • Standardized Size Hierarchy: Three carefully calibrated sizes — S (32×18dp), M (40×22dp), and L (48×28dp).
  • Smooth Interaction & Dark Mode: Smooth animated thumb transitions (150ms tween) with integrated ripple indications and automatic dark theme track/thumb color adaptation.

Import from com.ux4g.designsystemv1.components.toggle.*.


Interactive Previews

1. Default Toggle Switch (Standalone)

Standalone binary switch without accompanying label text.

Ux4gSwitch - Default StandaloneJetpack Compose
UX4G Default Standalone Toggle Switch Preview

2. Enabled Toggle Switch (On / Checked)

Standalone switch in the active/checked state with colored track and right-aligned thumb.

Ux4gSwitch - Enabled StandaloneJetpack Compose
UX4G Enabled Standalone Toggle Switch Preview

3. Toggle Switch with Label

Toggle switch paired with field label text, required asterisk (*), and informative trailing icon.

Ux4gSwitch - With LabelJetpack Compose
UX4G Toggle Switch with Label Preview

Kotlin Usage

1. Notification & SMS Preference Settings

NotificationPreferences.kt
import androidx.compose.foundation.layout.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.ux4g.designsystemv1.components.toggle.*
import com.ux4g.designsystemv1.foundation.Ux4gIcons

@Composable
fun NotificationPreferencesSection() {
    var smsAlerts by remember { mutableStateOf(true) }
    var emailDigest by remember { mutableStateOf(false) }
    var biometricLogin by remember { mutableStateOf(true) }

    Column(
        modifier = Modifier.padding(16.dp),
        verticalArrangement = Arrangement.spacedBy(16.dp)
    ) {
        // Standard switch with label, required asterisk, and description
        Ux4gSwitch(
            checked = smsAlerts,
            onCheckedChange = { smsAlerts = it },
            label = "SMS Application Status Alerts",
            description = "Receive instant transaction and dispatch updates via SMS",
            isRequired = true,
            trailingIcon = Ux4gIcons.info_circle,
            size = Ux4gSwitchSize.M,
            labelPosition = Ux4gSwitchLabelPosition.Right
        )

        // Compact Small switch
        Ux4gSwitch(
            checked = emailDigest,
            onCheckedChange = { emailDigest = it },
            label = "Weekly Digest Email",
            description = "Summary of portal announcements and new state schemes",
            size = Ux4gSwitchSize.S,
            labelPosition = Ux4gSwitchLabelPosition.Right
        )

        // Large prominent switch
        Ux4gSwitch(
            checked = biometricLogin,
            onCheckedChange = { biometricLogin = it },
            label = "Biometric Aadhaar Quick Login",
            description = "Unlock dashboard using registered device fingerprint",
            size = Ux4gSwitchSize.L,
            labelPosition = Ux4gSwitchLabelPosition.Right
        )
    }
}

2. Dual-Sided Label Switch (BothSides Layout)

DualSidedBillingToggle.kt
import androidx.compose.runtime.*
import com.ux4g.designsystemv1.components.toggle.*

@Composable
fun SchemeTenureSelector() {
    var isAnnual by remember { mutableStateOf(false) }

    Ux4gSwitch(
        checked = isAnnual,
        onCheckedChange = { isAnnual = it },
        label = "Quarterly Subsidy",
        secondaryLabel = "Annual Disbursal",
        size = Ux4gSwitchSize.M,
        labelPosition = Ux4gSwitchLabelPosition.BothSides
    )
}

Parameters

Ux4gSwitch Props

PropTypeDescriptionDefault
checked *BooleanWhether the toggle switch is currently in the active/checked state.—
onCheckedChange *((Boolean) -> Unit)?Callback triggered when the switch is toggled. Pass null for read-only.—
modifierModifierCompose layout modifier applied to the outer container.Modifier
labelString?Primary label text displayed next to the switch.null
descriptionString?Secondary supporting description text rendered below the primary label.null
sizeUx4gSwitchSizeSwitch dimensions: S (32×18dp), M (40×22dp), or L (48×28dp).Ux4gSwitchSize.M
labelPositionUx4gSwitchLabelPositionLayout placement: Right, Left, BothSides, or NoLabel.Ux4gSwitchLabelPosition.Right
enabledBooleanControls interactive touch response and disabled opacity.true
isRequiredBooleanWhen true, appends a red asterisk (*) to the primary label.false
trailingIconUx4gIcon?Optional icon rendered immediately after the primary label text.null
icon(@Composable () -> Unit)?Optional composable icon slot placed next to the primary label.null
labelStyleTextStyle?Custom typography style for primary label.null
descriptionStyleTextStyle?Custom typography style for supporting description text.null
secondaryLabelString?Secondary label text used when labelPosition = BothSides.null
isSecondaryRequiredBooleanAppends a red asterisk (*) to the secondary label.false
trailingSecondaryIconUx4gIcon?Trailing icon for secondary label when using BothSides.null
colorsUx4gToggleColors?Custom color palette overrides for track and thumb states.null
interactionSourceMutableInteractionSourceStream of interactions for ripple feedback.remember { MutableInteractionSource() }

Ux4gSwitchSize Reference

SizeTrack Width (Dp)Track Height (Dp)Thumb Diameter (Dp)Typical Usage
S32.dp18.dp14.dpDense table rows, compact bottom sheets, toolbars
M40.dp22.dp18.dpStandard mobile settings, citizen preference forms
L48.dp28.dp24.dpProminent featured switches, primary app mode toggles

Ux4gSwitchLabelPosition Reference

PositionLayout Behavior
NoLabelStandalone switch control without text labels.
RightStandard layout: Switch on left, primary label and description on right.
LeftInverted layout: Primary label and description on left, switch on right.
BothSidesDual-mode layout: Primary label on left, switch in middle, secondary label on right.

On this page