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.
13 Answers
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.
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
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"/> Comments
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
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
Kotlin version:
checkBox.buttonTintList = ColorStateList.valueOf(R.color.colorPrimary) 1 Comment
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
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
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