In today’s tutorial, we will learn about Checkbox in KMP Kotlin Compose Multiplatform for both Android & iOS platforms. We will discuss creating a Checkbox in KMP(KMM), handling the Checkbox state, customizing Checkbox properties, and showing Checkbox selected items in the Text widget.
Checkbox in KMP Kotlin Compose Multiplatform:
A Checkbox is a UI widget allowing application users to select or deselect an option. It allows us to select multiple options from a list by checking and unchecking states. The checkbox is mainly used in applications for Remember Me, Accepting terms and conditions agreements, and giving the users multiple choice selection boxes.
Implement Checkbox in KMP:
In Kotlin Compose Multiplatform, a widget named Checkbox is given to create material style Checkbox for both Android & iOS.
1 2 3 4 5 6 7 |
// State var isChecked by remember { mutableStateOf(false) } Checkbox( checked = isChecked, onCheckedChange = { isChecked = it } ) |
Code explanation:
- isChecked: State to hold the check and uncheck result in True and false format.
- checked: the value of the Checkbox.
- onCheckedChange: Calls every time the user checks or unchecks the Checkbox.
Source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.app.test import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.material.Checkbox import androidx.compose.material.MaterialTheme import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp @Composable fun App() { MaterialTheme { Box(modifier = Modifier.fillMaxSize().padding(16.dp)) { Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) { var isChecked by remember { mutableStateOf(false) } Checkbox( checked = isChecked, onCheckedChange = { isChecked = it } ) } } } } |
Screenshot
Adding Label to Checkbox in KMP:
To add a Label to CheckBox, we will wrap both Checkbox and Text into a Row widget. This will align both of them horizontally.
1 2 3 4 5 6 7 8 9 10 11 12 |
Row( verticalAlignment = Alignment.CenterVertically ) { Checkbox( checked = isChecked, onCheckedChange = { isChecked = it } ) Text( text = "Google", modifier = Modifier.padding(start = 8.dp) ) } |
Source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.app.test import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.material.Checkbox import androidx.compose.material.MaterialTheme import androidx.compose.material.Text import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp @Composable fun App() { MaterialTheme { Box(modifier = Modifier.fillMaxSize().padding(16.dp)) { Column( Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally ) { var isChecked by remember { mutableStateOf(false) } Row( verticalAlignment = Alignment.CenterVertically ) { Checkbox( checked = isChecked, onCheckedChange = { isChecked = it } ) Text( text = "Google", modifier = Modifier.padding(start = 8.dp) ) } Text( text = if (isChecked) "Checked" else "Unchecked", modifier = Modifier.padding(start = 8.dp) ) } } } } |
Screenshot:
Customizing Checkbox in KMP:
The Checkbox widget has color properties, such as CheckboxDefaults.colors(). It has 5 sub-properties: checkedColor, uncheckedColor, and checkmarkColor, disabledColor and disabledIndeterminateColor.
1 2 3 4 5 6 7 8 9 10 11 |
Checkbox( checked = isChecked, onCheckedChange = { isChecked = it }, colors = CheckboxDefaults.colors( checkedColor = Color(0xFF00BFA5), uncheckedColor = Color.Gray, checkmarkColor = Color.White, disabledColor = Color.Gray, disabledIndeterminateColor = Color.Gray ) ) |
Screenshot:
Checkbox with Multi-Selection Enabled in KMP:
Finally, we are creating multiple checkboxes with multiple selected enabled. We have a list of Strings with the list of checkbox enabled and disabled states. We show selected items in a Text on each selection and de-selection. With this, you can easily learn to get multiple selected items into a Single object in Checkbox.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// States val tasks = listOf("Google", "Yahoo", "Bing") val checkedStates = remember { mutableStateListOf(false, false, false) } tasks.forEachIndexed { index, task -> Row(verticalAlignment = Alignment.CenterVertically) { Checkbox( checked = checkedStates[index], onCheckedChange = { checkedStates[index] = it } ) Text( text = task, modifier = Modifier.padding(start = 8.dp), style = TextStyle(fontSize = 18.sp) ) } } |
Code explanation:
- tasks: Checkbox list items.
- checkedStates: Checkbox selected items state.
- tasks.forEachIndexed: To render Checkbox items from the list.
Complete source code for App.kt file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
package com.app.test import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.material.Checkbox import androidx.compose.material.CheckboxDefaults import androidx.compose.material.MaterialTheme import androidx.compose.material.Text import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp @Composable fun App() { MaterialTheme { val tasks = listOf("Google", "Yahoo", "Bing") val checkedStates = remember { mutableStateListOf(false, false, false) } Box(modifier = Modifier.fillMaxSize().padding(16.dp)) { Column { Text( "Checkbox in KMP (KMM) Kotlin Compose Multiplatform", style = TextStyle(fontSize = 24.sp), textAlign = TextAlign.Center ) Spacer(modifier = Modifier.height(16.dp)) // Task List with Checkboxes tasks.forEachIndexed { index, task -> Row(verticalAlignment = Alignment.CenterVertically) { Checkbox( checked = checkedStates[index], onCheckedChange = { checkedStates[index] = it }, colors = CheckboxDefaults.colors( checkedColor = Color(0xFF00BFA5), uncheckedColor = Color.Gray, checkmarkColor = Color.White, disabledColor = Color.Gray, disabledIndeterminateColor = Color.Gray ) ) Text( text = task, modifier = Modifier.padding(start = 8.dp), style = TextStyle(fontSize = 18.sp) ) } } Spacer(modifier = Modifier.height(16.dp)) // Display Selected Items val selectedTasks = tasks.filterIndexed { index, _ -> checkedStates[index] } Text( text = if (selectedTasks.isNotEmpty()) "Selected: ${selectedTasks.joinToString(", ")}" else "No items selected", style = TextStyle(fontSize = 16.sp, fontWeight = FontWeight.Bold) ) } } } } |
Screenshots in Android device:
Screenshot in iOS device: