Android CheckBox


Android CheckBox widget provides interface to select/deselect items. This tutorial will make you familiar with android checkbox, its declaration, clickListener.


DownloadSource Code :


Now Let us work on Project : (Refer – Configuring Existing AndroidStudioProject) CheckBox which you might have downloaded from above or you can follow the scripts below by copying it to implement in your Project. To launch Downloaded project, Extract the zip, copy it into AndroidStudioProjects. Now from Android Studio click open Existing Android Studio Project and select the path to extracted file we just copied in AndroidStudioProjects directory. If Sync SDK path prompt comes just click OK and you are ready to work. If still you get error check for AndroidStudioProjects/CheckBox/app/build.gradle , it will contain different SDK Version. Change it to SDK Version which you have.



1. activity_main.xml : Layout file


Explaining above Code Separately :

CheckBox – 1

  • Line 6 android:text assigns text string to our CheckBox.
  • Line 7 we can add colour to checkBox text using property android:textColor and providing 8-digit Hex color.


CheckBox – 2

  • Line 7 Using property android:checked=”true” we can make our CheckBox selected by default on page load.


CheckBox – 3

  • Line – 9 onClick=”click_handler_methodName” adds a click Listener, which listens to CheckBox selection – deselection. Over here “chk_bx3_clicked” method will be call from on user interaction with our checkBox. (This is done in similar manner we did for Android Button).


CheckBox – 4

  • We have simply declared a checkBox which is having OnClickListener inside onCreate() method of file ( Line:31-41 ).



Explaining above Code :

  • Line 13 & 21-24 We have declared 4 CheckBox, and linked them with CheckBox of XML Layout file.
  • Line 31-41 deals with setting click listener for 4th CheckBox. Line 31 we set a click listener and pass instance of OnClickListener class as a parameter to it.
  • Line 35 we override onClick method and using this we can catch every clicks over our CheckBox. Inside this we have placed a Toast which shows CheckBox text using chk_bx4.getText().toString() + selection state (whether it is checked or unchecked) using chk_bx4.isChecked().
  • Line 50-54 is another method to set click listener with on click handler method. For this first you need to declare the onClick handler method “chk_bx3_clicked()”  from (activity_main.xml) Layout XML file and then inside ( Java file add the handler method with same name that we declared in Layout file + passing View parameter since we are working on UI thread.
  • Line 52 We have placed a Toast which shows CheckBox text using chk_bx3.getText().toString() + selection state (whether it is checked or unchecked) using chk_bx3.isChecked().
  • Line 60-74 When user clicks on Button we get summary for all CheckBox from handler method btn_clicked(). Over here we have placed Toast inside which we check whether CheckBoxes are checked or unchecked.
  • Line 71-72 If we  want to deselect any CheckBox then we can use syntax : checkBox_Name.setChecked(false); Remove comment and see that when button is clicked if the 3rd & 4th CheckBox are checked they becomes unchecked. Its reverse is also possible if we wish to select our CheckBox then try checkBox_Name.setChecked(true); 


Final Output :

android chechBox


Do It Yourself :

Try selecting all four checkBox by default from file. For this simply inside onCreate() method add chkBx_name.setChecked(true);



Report Errors + Bugs & Become Insider for

We would like to hear you, if you find any error or misspelled phrase while reading our tutorials. By reporting mistakes through email to you could help other peers.