CheckBox in Android – Implementation and Overview

CheckBox in Android – Implementation and Overview

CheckBox is a important but but not very much used view in Android. You can allow the user to select one or multiple options using the checkbox and perform the respective action for it. Below is the implementation

Checkbox in android
Check Box in android
<CheckBox
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

 

Just like any other view, layout_width and layout_height are default properties of checkbox.

 

CheckBox – Other important properties

android:id – this is very important as there is a need to recognise which check box is selected by the user

android:text – Gives a label to the check box (Typically arrange it in a vertical layout to make it look clearer)

android:onClick – Can call a public method through this to perform action

android:checked – Gives a default value to the checkbox

android:background – This is to give a drawable for background

CheckBox in Android
Sample CheckBox in Android

This is a simple check box that i created for this tutorial. Can you guess what property i used here!!

 

<CheckBox
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="sample1"
    android:id="@+id/samplecheck1" />

<CheckBox
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="sample2"
    android:checked="true"
    android:id="@+id/samplecheck2"/>

The above two checkbox with one difference only, android:checked is set for Check box 2. It is important to know what properties you are using to create a neat check box.

 

CheckBox in Activity

Once you have created a check box, you need to set individual listeners to it. Since, multiple check box can be chosen by the user, we need to handle that situation. Sample listener to handle this situation is given below.

CheckBox checkBox1 = (CheckBox) findViewById(R.id.samplecheck1);
CheckBox checkBox2 = (CheckBox) findViewById(R.id.samplecheck2);
checkBox1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        onCheckboxClicked(View view)
    }
});
checkBox2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        onCheckboxClicked(View view)
    }
});
public void onCheckboxClicked(View view) {
    // Is the view now checked?
    boolean checked = ((CheckBox) view).isChecked();

    // Check which checkbox was clicked
    switch(view.getId()) {
        case R.id.samplecheck1:
            if (checked)
                //perform some action
           
            break;
        case R.id.samplecheck2:
            if (checked)
                // perform action            
            break;
        
    }
}

 

Tips to create the perfect Check Box

It is always important to know about the best practices for creating a checkbox. Check this material design project by google, where some neat and cleaner looking check boxes are created.

  • It is important to keep in mind that, the text associated with the checkbox is clearly present.
  • Do not model the check box with a crude design in mind. Always model first.
  • Plan the content for the check boxes before hand and then think about placing them vertically (Horizontal placement should be avoided as much as possible)
  • Make sure the text values of each boxes are almost of the same length (Not too short not too long).

 

Learn to incorporate this into your design. Be sure to first create a wireframe design and then model the application based on that.

I have created a tutorial here with list of things you will need to create a professional looking application. Take a look at it.

 

“Learn be Curious”.

Leave a Comment