Wednesday, 21 August 2013

Customizing the checkbox in Android

The checkbox in Android comes with two flavours: The simple Checkbox or CheckedTextView, which has an TextView attached on it.

1. Create a new drawable
To customized both of them we need a new xml file in any of the folder of drawable. Here is an example called customized_checkbox.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_activated="true" android:drawable="@drawable/checkbox_selected"/>
<item android:state_activated="false" android:drawable="@drawable/checkbox_deselected" />

        <item android:state_checked="false" android:drawable="@drawable/checkbox_deselected" />
<item android:state_checked="true" android:drawable="@drawable/checkbox_selected" />
</selector>
*Note: The reason why it has the state activated is because for the list view, if the row is not and instance of checkable, after the HoneyComb it will be "activated". Check the reason here:
StackOverFlow - What is the difference between the states selected, checked and activated in Android

2. Customize the checkbox
For the simple checkbox, the attribute button will be used:

android:button="@drawable/customized_checkbox"

And for the CheckedTextView use the attribute checkMark:

android:checkMark="@drawable/customized_checkbox"

For more information

No comments:

Post a Comment