Open In App

Checkbox in Android using Jetpack Compose

Last Updated : 25 Feb, 2021
Like Article

The checkbox is a composable function that is used to represent two states of any item in Android. It is used to differentiate an item from the list of items. In this article, we will take a look at the implementation of Simple Checkbox in Android using Jetpack Compose. 

Attributes of Checkbox



checked  this is used to set our checkbox checked or unchecked on app launch.
onCheckedChange  this is a callback that will receive when there is a change in events whether the checkbox is checked or unchecked.
modifier  this is used to add beautification to our checkbox in the sense of padding, margin, and other properties. 
color  this parameter is used to add color to our checkbox in default case the checkbox color is secondary color in the app theme.

Step by Step Implementation

Step 1: Create a New Project

To create a new project in the Android Studio Canary Version please refer to How to Create a new Project in Android Studio Canary Version with Jetpack Compose.

Step 2: Working with the MainActivity.kt file

Navigate to the app > java > your app’s package name and open the MainActivity.kt file. Inside that file add the below code to it. Comments are added inside the code to understand the code in more detail.


import android.os.Bundle
import android.widget.Toast
import androidx.compose.material.*
import androidx.compose.material.Icon
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.Phone
import androidx.compose.runtime.*
import androidx.compose.runtime.savedinstancestate.savedInstanceState
import androidx.compose.ui.Alignment
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.setContent
import androidx.compose.ui.res.imageResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.gfgapp.ui.GFGAppTheme
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.ContextAmbient
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.semantics.SemanticsProperties.ToggleableState
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.input.*
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        setContent {
            Column {
                // in below line we are 
                  // calling a checkbox method. 
@Preview(showBackground = true)
fun DefaultPreview() {
    GFGAppTheme {
fun SimpleCheckboxComponent() {
    // in below line we are setting
     // the state of our checkbox.
    val checkedState = remember { mutableStateOf(true) }
    // in below line we are displaying a row
      // and we are creating a checkbox in a row. 
    Row {
            // below line we are setting 
              // the state of checkbox.
            checked = checkedState.value,
            // below line is use to add padding
              // to our checkbox. 
            modifier = Modifier.padding(16.dp),
            // below line is use to add on check 
              // change to our checkbox. 
            onCheckedChange = { checkedState.value = it },
        // below line is use to add text to our check box and we are 
         // adding padding to our text of checkbox
        Text(text = "Checkbox Example", modifier = Modifier.padding(16.dp))

Now run your app and see the output of the app.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads