How to change check box tick color in android
Asked Answered
M

13

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.

Metrology answered 12/12, 2013 at 11:48 Comment(1)
Here is the solution: check this outNormally
E
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.

Evonevonne answered 12/12, 2013 at 11:57 Comment(3)
this made my checkbox disappear O_OInheritrix
you will have to have border around images to show box, one image will be empty border and other with the tick imageHeart
Disappear because u used color changes in android:drawable="@drawable/checked". U need image!Lesslie
M
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.

Misbeliever answered 8/2, 2016 at 5:58 Comment(0)
J
12

As of API 21 you can use the Button Tint attribute

android:buttonTint="#FFFF00"
Jeunesse answered 19/10, 2015 at 18:26 Comment(0)
S
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. :)

Schutz answered 9/5, 2019 at 21:42 Comment(2)
Also in xml: android:buttonTint="@color/arancioneMedium"Fedora
I had to use: cb.setButtonTintList(ColorStateList.valueOf(getResources() .getColor(R.color.colorAccent, null)));Largescale
P
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"/>
Primitivism answered 1/3, 2021 at 7:2 Comment(0)
D
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

Daydream answered 11/7, 2015 at 6:58 Comment(3)
seriosly your comment works fine but what have you written in your answerUtham
Wouldn't changing your theme change way more than just the checkboxes?Waikiki
@Nicolas Carrasco Well, as long as you create a id for this style and apply it only to the check-boxes it wouldn't...Veratridine
D
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>
Dendrology answered 29/3, 2020 at 19:5 Comment(0)
C
1

Kotlin version:

checkBox.buttonTintList = ColorStateList.valueOf(R.color.colorPrimary)
Calling answered 23/4, 2021 at 9:17 Comment(1)
Note: valueOf() takes a color value, not a color resource.Damson
S
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.

Spalla answered 12/12, 2013 at 11:51 Comment(0)
A
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://mcmap.net/q/267370/-how-to-change-the-check-box-tick-box-color-to-white-in-android-xml

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

Auspicious answered 18/4, 2018 at 12:4 Comment(0)
A
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))

Aeolian answered 20/8, 2020 at 10:30 Comment(0)
L
0

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

This is working with png.

Leonie answered 19/8, 2021 at 19:58 Comment(0)
N
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
Natatorial answered 26/2 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.