What is Check box?
Checkboxes are just like the buttons that allow the users to select one or more options from a set. We use checkbox options in a vertical list.
It can also be used to turn an option on or off for instance to on or off the flash light of our smartphones.
Checkbox is a two-states button checked (selected) or unchecked (not selected).
Checkboxes can have a parent-child relationship with other checkboxes. It means When the parent checkbox is checked, all the child checkboxes are checked. If a parent checkbox is unchecked, all child checkboxes are unchecked. If some, but not all, child checkboxes are checked, the parent checkbox becomes an indeterminate checkbox.
Also Read Floating Action Button in Jetpack Compose
Steps :
Create a New Project and select Empty Activity In my case, application name is Coding Bihar you can use any other name.
Also Read Radio Button in Jetpack Compose
MainActivity
Copy this code →
package com.example.codingbihar
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import com.example.codingbihar.ui.theme.CodingBiharTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
CodingBiharTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Checkboxes()
}
}
}
}
Create a separate file for Checkbox and named it as CheckBox
CheckBox
Copy this code →
package com.example.codingbihar
import android.widget.Toast
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Checkbox
import androidx.compose.material3.CheckboxDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
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.unit.dp
@Composable
fun Checkboxes() {
val checkboxList: List = listOf("Checkbox 1", "Checkbox 2", "Checkbox 3","Checkbox 4")
val context = LocalContext.current.applicationContext
Column(horizontalAlignment = Alignment.Start) {
checkboxList.forEach { optionName ->
var checked by remember {
mutableStateOf(false)
}
Row(verticalAlignment = Alignment.CenterVertically) {
Checkbox(
checked = checked,
onCheckedChange = { checked_ ->
checked = checked_
Toast.makeText(context, "$optionName $checked_", Toast.LENGTH_SHORT)
.show()
},
colors = CheckboxDefaults.colors(
checkedColor = Color.Magenta)
)
Text(
modifier = Modifier.padding(start = 2.dp),
text = optionName
)
}
}
}
}
OUTPUT :
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhaZ9x6s91eCaKS11OQJspnvz6Gt-dKFwKdaohdj8SR_zVmjZOzlzSasYDk2csP68hQJgFtlgt3PrQYVbY8RhSCOLCE_-Y_nAoEIGX1yiDip1D44M1tWynLKmlFqxn3poYVnDV6IXjGfWvv46IU0wqJFwPgewgCncGX28mpiVYW3KTOP16GkF81JK8Oag/w296-h640-rw/Checkbox4.webp)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjq37MoYz8yeU5yI0087rNUPs-f3GrhymkA6HCYLupCFTpAtqXacM1QwHXI8DGM55nr2QQCsAwc_kIHs_iaK981u8RIXLRcAFiLP7Q1zyeLYtEJI8zGHcyzcqKGcWd2MjjhnWTH-3Lm0WYTRaUFfKn0yoNFjSuhi7xZEbj5WFBfgmtQo50xbqFY1qglJA/w296-h640-rw/Checkbox3.webp)
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHLO4mpxJ7_vPwIYBUjinVUK2bVkK44IKON_WaJXQ0NzTaUVne8AeifWXa1Bfp-M9XtWx3CzLtvxW9AGLybRrrluUfqcvQBRSAsTu6EtakT12s-XPvTNitNo_qTcEnVDrV3m8qPPs2NVJA0dIYk6uGQMksAT8RHVd3mIAwChpMIMrPsfQnqZPf83magQ/w296-h640-rw/Checkbox2.webp)
*******************End**********************************
![Sandeep Gupta](http://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEik2MuJw6Nf2-H9oGwPbg9a1QMtCY8PfIaaqNzq0234pnt65zt6Hary1M9RhmPv54pxWQarfRKzCfvrLFMXcH-tToe0LJHW8VCbi28zIjF4w-LifXw7BdSzNCII5QyqNyFAF2fjPn-VJej9q6e0xG1_rbeireNQegXwm21kLDDvaKqy8A/w130/author%20(1).png)