Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Default Color of Android CheckBox Check Mark

How do I change the default color of the Android checkbox from green checkmarks to blue for a particular CheckBox?

like image 981
Code Droid Avatar asked Jul 02 '12 23:07

Code Droid


1 Answers

Unfortunately, changing the colour isn't a simple attribute. The checkmark is an image, so you have to create a custom image. Take a look at this example

Create a selector xml file such as this:

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

save this xml file in your res\drawables\ folder. Then inside your layout file apply it to your checkBox like this:

<CheckBox
    android:text="Custom CheckBox"
    android:button="@drawable/checkbox_selector"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

In this example you'd name your selector xml file "checkbox_selector.xml" and you'd need a star_down.png, and star.png in your drawables folder as well. You can use this technique to create different colored checkboxes by altering the system checkbox images to whatever color you want and referencing the altered png files in a selector.

like image 194
HexAndBugs Avatar answered Sep 19 '22 18:09

HexAndBugs