Articles → ANDROID → Checkbox Control In Android

Checkbox Control In Android






What Is Checkbox Control?





Example




  1. Create 2 checkboxes.
  2. If any of the checkbox is checked then a toast will come that checkbox is checked.
  3. If any of the checkbox is unchecked then a toast will come that checkbox is unchecked.


Picture showing 2 checkboxes inside the linear layout in android
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();
}



Posted By  -  Karan Gupta
 
Posted On  -  Wednesday, November 20, 2019

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250