Switch Button in Android using Jetpack Compose
Last Updated : 28 Feb, 2025
A Switch or a Switch Button in Android is a UI element that is used to switch between two states upon click. It can be assumed as a Boolean button with two different values. Some states where you may find a Switch in your Android device can be WIFI ON and OFF, Bluetooth ON and OFF, Dark Mode and Light Mode, etc.

In this article, we will show you how you could implement a Switch Button and check its Checked State in Android using Jetpack Compose. Follow the below steps once the IDE is ready.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version.
We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
MainActivity.kt:
Kotlin package org.geeksforgeeks.demo import android.os.Bundle import android.widget.Toast import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge import androidx.compose.foundation.layout.* import androidx.compose.material3.* import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import com.subhajitrajak.demo.ui.theme.DemoTheme class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { DemoTheme { Scaffold { contentPadding -> MyContent(Modifier.padding(contentPadding)) } } } } } @Composable fun MyContent(modifier: Modifier = Modifier) { val mContext = LocalContext.current val mCheckedState = remember { mutableStateOf(false) } Column( modifier = modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center ) { Switch( checked = mCheckedState.value, onCheckedChange = { mCheckedState.value = it } ) Spacer(modifier = Modifier.height(100.dp)) Button( onClick = { Toast.makeText(mContext, mCheckedState.value.toString(), Toast.LENGTH_SHORT).show() } ) { Text("Show Checked State", color = Color.White) } } } @Preview(showBackground = true) @Composable fun DefaultPreview() { MyContent() }
Output:
When you run the application you will see that the Switch is initially false. When you click the Button, it will display a Toast with 'false' written. Now, when you click on Switch, it becomes true and if the Button is clicked, it will display a Toast with 'true' written. You can see this happening in the below video of this application.