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

Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
user112016322
  • 688
  • 3
  • 11
  • 26

7 Answers7

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
}
sud007
  • 5,824
  • 4
  • 56
  • 63
24

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);
}
ywwynm
  • 11,573
  • 7
  • 37
  • 53
  • 7
    The last line should be 'CompoundButtonCompat.setButtonTintList(checkBox, colorStateList)' – Matt W Jul 27 '17 at 18:28
  • If it doens't work, wrap `uncheckedColor` with `ContextCompat.getColor(checkBox.getContext, uncheckedColor)` and the same with checkedColor – Linxy Aug 27 '17 at 17:12
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 :)

ladytoky0
  • 588
  • 6
  • 16
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" />
Amit Sinha
  • 93
  • 4
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)
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Tarif Chakder
  • 1,708
  • 1
  • 11
  • 10
0

It works well for me (kotlin)*

if (isChecked){
            checkBox.buttonTintList = ColorStateList.valueOf(WHITE)
        }else{
            checkBox.buttonTintList = ColorStateList.valueOf(RED)
        }
Maruf Alam
  • 228
  • 4
  • 10
-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"
Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30