Checkbox
Notes
Checkboxes allow users to select multiple options from a set. Use checkboxes when the user needs to see all available options. If the user needs to select only one option from a set, use radio buttons instead.
Checkboxes Example
class MainActivity : AppCompatActivity() {
private lateinit var checkBoxAndroid: CheckBox
private lateinit var checkBoxWeb: CheckBox
private lateinit var checkBoxAI: CheckBox
private lateinit var btnSubmit: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
checkBoxAndroid = findViewById(R.id.checkBoxAndroid)
checkBoxWeb = findViewById(R.id.checkBoxWeb)
checkBoxAI = findViewById(R.id.checkBoxAI)
btnSubmit = findViewById(R.id.btnSubmit)
btnSubmit.setOnClickListener {
val selectedOptions = mutableListOf<String>()
if (checkBoxAndroid.isChecked) selectedOptions.add("Android Development")
if (checkBoxWeb.isChecked) selectedOptions.add("Web Development")
if (checkBoxAI.isChecked) selectedOptions.add("Artificial Intelligence")
if (selectedOptions.isNotEmpty()) {
Toast.makeText(this, "Selected: ${selectedOptions.joinToString(", ")}", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "No options selected", Toast.LENGTH_SHORT).show()
}
}
}
}