Articles → ANDROID → Checkbox Control In Android
Checkbox Control In Android
What Is Checkbox Control?
Example
- Create 2 checkboxes.
- If any of the checkbox is checked then a toast will come that checkbox is checked.
- If any of the checkbox is unchecked then a toast will come that checkbox is unchecked.
Click to Enlarge
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="horizontal" tools:context=".debug.EditTextDemo">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="horizontal">
<CheckBox android:id="@+id/checkbox2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:onClick="CheckboxClicked"
android:text="CheckBox2" />
<CheckBox
android:id="@+id/checkbox1" android:layout_width="wrap_content"
android:layout_height="43dp" android:onClick="CheckboxClicked" android:text="CheckBox1" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
public void CheckboxClicked(View view) {
CheckBox checkBox = (CheckBox) view;
if (checkBox.isChecked())
Toast.makeText(this, checkBox.getText() + " checked", Toast.LENGTH_LONG).show();
else
Toast.makeText(this, checkBox.getText() + " unchecked", Toast.LENGTH_LONG).show();
}