14

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

Code Droid
  • 10,344
  • 17
  • 72
  • 112
  • 1
    Seems to be a duplicate of [this question](http://stackoverflow.com/questions/3491203/how-to-customize-the-color-of-the-checkmark-color-in-android-in-a-dialog-andr). – nTraum Jul 02 '12 at 23:23
  • 3
    The difference is this question has a real and new answer. – Code Droid Jul 03 '12 at 00:27
  • Possible duplicate of [How to change the color of a CheckBox?](http://stackoverflow.com/questions/5854047/how-to-change-the-color-of-a-checkbox) – afathman Feb 04 '16 at 21:50

2 Answers2

22

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.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
HexAndBugs
  • 5,549
  • 2
  • 27
  • 36
  • 1
    While the example you link is a good one. It is worth noting that Links do go dead, generally in answers with links you'll want to post the relevant content from the link as well. Since this answer is going to be around for a long time it would have become useless to people in the future if the link had rotted. Now it will still help them achieve what they are after. – FoamyGuy Jul 02 '12 at 23:48
8

This is easy to do in xml using buttonTint (as of API level 23):

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:buttonTint="@color/COLOR_HERE" />

and as Nicolás pointed out, you can do this using appCompatCheckbox v7 for older APIs:

<android.support.v7.widget.AppCompatCheckBox 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:buttonTint="@color/COLOR_HERE" /> 
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
afathman
  • 5,993
  • 2
  • 20
  • 28