How to change checkbox checked color programmatically

27,678

Solution 1

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"

Solution 2

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
}

Solution 3

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

Solution 4

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

Share:
27,678
user112016322
Author by

user112016322

Updated on July 11, 2022

Comments

  • user112016322
    user112016322 almost 2 years

    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

  • Boon
    Boon over 7 years
    This solution won't work for API level 17 (and possibly anything below API 21)
  • Matt W
    Matt W almost 7 years
    The last line should be 'CompoundButtonCompat.setButtonTintList(checkBox, colorStateList)'
  • Linxy
    Linxy over 6 years
    If it doens't work, wrap uncheckedColor with ContextCompat.getColor(checkBox.getContext, uncheckedColor) and the same with checkedColor
  • Antimonit
    Antimonit about 6 years
    Actually, the first branch is enough by itself. No need to check for the SDK version at all.