43

I am developing android application In that i use check box but default check box tick color is blue so i want to change that color to yellow. is there any inbuilt property to set color to check box tick.

Atul K
  • 483
  • 2
  • 5
  • 11
  • 2
    Here is the solution: [check this out](http://stackoverflow.com/questions/3192173/change-icons-of-checked-and-unchecked-for-checkbox-for-android) – Phantômaxx Dec 12 '13 at 11:50

12 Answers12

44

Unfortunately, changing the color of checkbox check mark isn't a simple attribute

Create a selector xml file in res\drawables\ folder with name cb_selector.xml

<?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/checked" />
    <item android:state_checked="false" android:drawable="@drawable/unchecked" />
</selector>

In your layout file apply this file to your checkBox

<CheckBox
    android:id="@+id/cb"
    android:text="My CheckBox"
    android:button="@drawable/cb_selector"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

Add a unchecked.png, and checked.png in your drawables folder. These are checked and unchecked image of checkbox.

Vaibhav Agarwal
  • 4,499
  • 3
  • 19
  • 20
28

You can use the attribute app:buttonTint of the AppCompatCheckBox from the android.support.v7 library.

<android.support.v7.widget.AppCompatCheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:buttonTint="@color/colorAccent"/>

Advantage: works also below API 21 and you don't need to redraw the checkbox.

DeRagan
  • 22,827
  • 6
  • 41
  • 50
dabo248
  • 3,367
  • 4
  • 27
  • 37
11

As of API 21 you can use the Button Tint attribute

android:buttonTint="#FFFF00"
Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
6

If you want to do this programmatically, then you simply do this:

final CheckBox cb = new CheckBox(getApplicationContext());
cb.setButtonTintList(getColorStateList(R.color.colorAccent));

Chris Stillwell's answer gave me the idea to try this as I couldn't simply set the colour using the attributes. :)

Carl Du Plessis
  • 325
  • 3
  • 6
  • 1
    Also in xml: `android:buttonTint="@color/arancioneMedium"` – sara Feb 02 '20 at 21:06
  • I had to use: `cb.setButtonTintList(ColorStateList.valueOf(getResources() .getColor(R.color.colorAccent, null)));` – gig6 Jan 13 '21 at 18:10
3

Go to styles.xml and add this line.

<style>
<item name="colorAccent">@android:color/holo_green_dark</item> 
</style>

using this you can change color or set different color

Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81
2

If you want to change only tint color than must go with the below solution. Its work perfectly. Create a Selector "check_box_tint.xml" in your res/drawable folder.

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

Now Use this drawable as color of your checkbox tint.

<CheckBox
   android:id="@+id/cbSelectAll"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:buttonTint="@drawable/check_box_tint"/>
Gunavant Patel
  • 1,413
  • 1
  • 13
  • 17
1

Kotlin version:

checkBox.buttonTintList = ColorStateList.valueOf(R.color.colorPrimary)
Jonas Simonaitis
  • 170
  • 1
  • 13
0

Use Custom selector for the checkbox.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/patch_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/patch_normal" android:state_enabled="true"/>
    <item android:drawable="@drawable/patchdisable" android:state_enabled="false"/>


</selector>

Like this.

Ashwin S Ashok
  • 3,623
  • 2
  • 29
  • 36
0

For those still looking for an answer (I am aware this is an older question) – I found this solution works well without having to worry about API: https://stackoverflow.com/a/31840734/7601437

In short: create a style for the checkbox, e.g. checkboxStyle and then implement it as a theme: android:theme="@style/checkboxStyle"

atschpe
  • 121
  • 14
0

Firstly, we must create a drawable that include checked and uncheck color situations, then you must set this drawable as buttonTint;

drawable_checkbox;

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="@color/kelleyGreen" />
    <item android:state_checked="false" android:color="@color/warmGrey" />
</selector>
<style name="CheckBox" parent="Widget.AppCompat.CompoundButton.CheckBox">
    <item name="android:textAppearance">@style/TextAppearance.Regular.XSmall</item>
    <item name="android:textColor">@color/warmGrey</item>
    <item name="buttonTint">@drawable/drawable_checkbox</item>
</style>
Özer Özcan
  • 1,253
  • 16
  • 17
0

For applying color programmatically it will require API Level >19 if your min sdk is >19 then you can use

checkbox[i]!!.setButtonTintList(getColorStateList(activity!!,R.color.green))

 OR

view.setButtonTintList(getColorStateList(activity!!,R.color.green))

Adam Noor
  • 101
  • 3
0

If nothing works than use AppCompatCheckBox with app:buttonCompat="your_drawable_selector"

This is working with png.

Andrain
  • 872
  • 1
  • 16
  • 43