45

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.

1
  • 2
    Here is the solution: check this out Commented Dec 12, 2013 at 11:50

13 Answers 13

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.

Sign up to request clarification or add additional context in comments.

3 Comments

this made my checkbox disappear O_O
you will have to have border around images to show box, one image will be empty border and other with the tick image
Disappear because u used color changes in android:drawable="@drawable/checked". U need image!
29

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.

Comments

12

As of API 21 you can use the Button Tint attribute

android:buttonTint="#FFFF00" 

Comments

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. :)

2 Comments

Also in xml: android:buttonTint="@color/arancioneMedium"
I had to use: cb.setButtonTintList(ColorStateList.valueOf(getResources() .getColor(R.color.colorAccent, null)));
4

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"/> 

Comments

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

3 Comments

seriosly your comment works fine but what have you written in your answer
Wouldn't changing your theme change way more than just the checkboxes?
@Nicolas Carrasco Well, as long as you create a id for this style and apply it only to the check-boxes it wouldn't...
1

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> 

Comments

1

Kotlin version:

checkBox.buttonTintList = ColorStateList.valueOf(R.color.colorPrimary) 

1 Comment

Note: valueOf() takes a color value, not a color resource.
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.

Comments

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"

Comments

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))

Comments

0

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

This is working with png.

Comments

0

For me this worked very well for checkbox selected and unselected color change

val states = arrayOf( intArrayOf(-android.R.attr.state_checked), intArrayOf(android.R.attr.state_checked) ) val colors = intArrayOf( requireContext().getColor(R.color.unchecked_color), requireContext().getColor(R.color.checked_color) ) val colorStateList = ColorStateList(states, colors) checkbox.buttonTintList = colorStateList 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.