Checkboxes allow the user to select one or more options from a set.
In this tutorial, we show you how to customize a checkbox, add a click listener by a few different ways, and how to get the setOnCheckedChangeListener for checkbox.
This project is developed in Eclipse 188.8.131.52. Make a custom checkbox by XML Layout
We need a xml file “checkbox_selector” in folder drawable:
And of course two image with name “check” and “uncheck” for two state of checkbox.
2. Two ways to attach a click listener to the checkbox, when user click on the checkbox, show info on the textview control
2.1. Use android:onClick attribute in your checkbox XML definition:
Within the activity that hosts this layout, the following method handles the click event for both checkboxes:
2.2. Or using an OnClickListener
3. How to get the setOnCheckedChangeListener for checkbox. This method is executed when there is any change in the state of the checkbox. You can change the state of checkbox from a button like:
And if you want to do something when state of checkbox is changed, you want to some code below:
You can download all source codes from here