Android CheckBox allows us to select one or more than one option from a set of options.
Android CheckBox is an on/off switch that can be toggled by the user. We can use check boxes when displaying users with a group of selectable options that are not mutually exclusive.
An Android CheckBox can be defined in xml layout as follows:
Android provides several text decorating attributes like Typeface, font family, text style, text size, text color, text shadow etc. that can be applied to CheckBox. To learn more on text styling visit below given link:
Here are some commonly used xml attributes which we use very frequently:
|android:background||We can use this attribute to set color or drawable background|
|android:paddingLeft||Can be used to assigned padding between check box and text|
|android:onClick||This is the name of the method in this View's context to invoke when the view is clicked.|
|android:checked||If attribute has a value "true" then by default it will be displayed as checked. For "false" value it will be displayed as unchecked|
|android:visibility||it controls the initial visibility of the view|
|android:drawableLeft||Used to show a drawable at left side of CheckBox|
|android:drawableRight||Used to show a drawable at right side of CheckBox|
|android:drawableTop||Used to show a drawable at the top of CheckBox|
|android:drawableBottom||Used to show a drawable at the bottom of CheckBox|
|android:text||This is the Text that we want to display for a CheckBox.|
Sometimes we need to check or uncheck a check box on runtime. We can check or uncheck an Android CheckBox as follows:
Here are simple steps to create a button selector to customize a CheckBox:
Step 1: Add 2 images in drawable folder, one for checked state of radio button and second one for uncheck state of button. I used 2 images for this purpose named checkbox_checked.png and checkbox_unchecked.png
Step 2: Create a file under res -> drawable and named it radiobutton_selector.xml
Step 3: Replace your res ->drawable ->radiobutton_selector.xml with below given code of my checkbox_selector.xml
Step 3: Set the android:button attribute of check box where you want this custom look & feel as follows:
Here are simple steps to create a color selector to customize a CheckBox:
Step 1: Define 2 colors that you want to use for checked and unchecked state of a Check Box
Step 2: Craete an xml file under res -> drawable and named it checkbox_color_selector.xml
Step 3: Open your res -> drawable -> checkbox_color_selector.xml and replace it's code with below given code:
Step 4: Assign android:textColor of check box to this drawable color selector as given below:
There are 4 different ways to handle click event of a view. In this example we are going to explain only one way to handle click event of check box. To learn more, what are other ways to handle click event of a view you can visit below given link.
A simple way to handle click event of a check box is to define onClick attribute of RadioButton as follows:
Now create a method named customCheckBoxClickHander inside Activity class that hold the radio button layout. This method must be public, Return void and accept View as a parameter as follows:
Sometimes we need to know checked radio button on some event say on a button click. Here is a simple way to check whether a checkbox is checked or not.
Alright, this approach is quite straight forward to check whether a checkbox is checked or not. It is useful when you are defining checkboxes in xml layout and you know the id of each Check box.
Suppose a case that you are getting some data from server, based on that data you have add checkboxes on runtime. Now, you have no idea how many checkboxes are there in that page and what are there ids. So, how we can get all the check box that has been checked by user?
If there is a case where you dont have any idea about number of checkboxes and their ids and you have to processed them on runtime to get all checked Checkbox then a better approach is to wrap all checkboxes in a container say liner layout. On runtime get all the child of parent container and traverse one by one to get all checked values
Let's quickly implement a sample for Android CheckBox, which demonstrate everything we discussed about Android CheckBox.
Step 1: Create a new project in Android Studio and name it Android CheckBox.
Step 2: Open res -> layout -> activity_main.xml and add following code
Step 3: Open res -> drawable and place 2 different images for different states of check box. Images of my sample is checkbox_checked.png, checkbox_unchecked.png
Step 4: Create res -> drawable -> checkbox_selector.xml and add following code
Step 5: Open res -> values -> colors.xml and replace its code with below given code of colors.xml
Step 6: Open res -> values -> strings.xml and replace its code with below given code of strings.xml
Step 7: Create a file checkbox_color_selector.xml under res -> drawable, and add below given code to your newly created file:
Step 8: Add below given code to your MainActivity.java
Step 9: Clean and run the project to see the result.