How to change check box tick color in android

Viewed 80943

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.

12 Answers

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

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

Kotlin version:

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

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>

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

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

This is working with png.

Related