15

I'm using the CheckBox view in Android. I would like to change the color of it when its checked. Right now its that default dark green color when its checked and I would like to change it to something different and when not checked, just be the default colors.

Here's my code:

CheckBox c = new CheckBox(this); c.setId(View.generateViewId()); c.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(buttonView.isChecked()) { buttonView.setBackgroundColor(Color.rgb(64, 131, 207)); } if(!buttonView.isChecked()) { buttonView.setBackgroundColor(Color.WHITE); } } }); 

The problem is that it does not change the right thing. Any ideas on how to change this color?

enter image description here

7 Answers 7

40

To tint the CompoundButton Tints try this, for Both the API>21 and below.

if (Build.VERSION.SDK_INT < 21) { CompoundButtonCompat.setButtonTintList(button, ColorStateList.valueOf(tintColor));//Use android.support.v4.widget.CompoundButtonCompat when necessary else } else { button.setButtonTintList(ColorStateList.valueOf(tintColor));//setButtonTintList is accessible directly on API>19 } 
Sign up to request clarification or add additional context in comments.

1 Comment

Actually, the first branch is enough by itself. No need to check for the SDK version at all.
25

Replace your CheckBox with AppCompatCheckBox and call following method:

public static void setCheckBoxColor(AppCompatCheckBox checkBox, int uncheckedColor, int checkedColor) { ColorStateList colorStateList = new ColorStateList( new int[][] { new int[] { -android.R.attr.state_checked }, // unchecked new int[] { android.R.attr.state_checked } // checked }, new int[] { uncheckedColor, checkedColor } ); checkBox.setSupportButtonTintList(colorStateList); } 

2 Comments

The last line should be 'CompoundButtonCompat.setButtonTintList(checkBox, colorStateList)'
If it doens't work, wrap uncheckedColor with ContextCompat.getColor(checkBox.getContext, uncheckedColor) and the same with checkedColor
3

In kotlin you can just write this:

refBtn.buttonTintList = ColorStateList.valueOf(Color.GRAY) 

You change change 'Color.GRAY' by the color you want, or write Color.rgb() and define the color :)

Comments

0
implement this file in res <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:state_focused="true" android:drawable="@drawable/checkbox_on_background_focus_yellow" /> <item android:state_checked="false" android:state_focused="true" android:drawable="@drawable/checkbox_off_background_focus_yellow" /> <item android:state_checked="false" android:drawable="@drawable/checkbox_off_background" /> <item android:state_checked="true" android:drawable="@drawable/checkbox_on_background" /> </selector> and then add button to checkbox <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="new checkbox" android:background="@drawable/checkbox_background" android:button="@drawable/checkbox" /> 

Comments

0

This is kotlin extension

fun AppCompatCheckBox.setTintAuto(enabledColor : Int, disabledColor : Int) { val colorTint = ColorStateList( arrayOf( intArrayOf(-R.attr.state_checked), intArrayOf(R.attr.state_checked) ), intArrayOf(enabledColor , disabledColor ) ) this.buttonTintList = colorTint this.setTextColor(colorTint ) } 

Then use it

checkBox.setTintAuto(activeColor,disabledColor) 

Comments

0

It works well for me (kotlin)*

if (isChecked){ checkBox.buttonTintList = ColorStateList.valueOf(WHITE) }else{ checkBox.buttonTintList = ColorStateList.valueOf(RED) } 

Comments

-4

Did you try to create a selector and assign this selector to your CheckBox like this for example:

//drawable file called cb_selector <?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/myCheckBox" android:text="My CheckBox" android:button="@drawable/cb_selector" android:layout_width="wrap_content" android:layout_height="wrap_content"/> 

@drawable/checked and @drawable/unchecked are two images of your checkbox, so you could put there a color that you want

OR without changing the button layout, with adding this attribute to your checkbox

android:buttonTint="@color/YOUR_CHECKMARK_COLOR_HERE" 

1 Comment

This solution won't work for API level 17 (and possibly anything below API 21)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.