15

I'm creating an ordinary Checkbox view:

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

Enter image description here

This light green (#A5D6A7) is due the accent color defined in the main style:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorAccent">@color/green_light</item>

I already found that I can't change this style at runtime: How to set colorAccent in code?

I want to change this color on a specific Checkbox, not globally over the app. Can I do it without creating a specific asset? Because the user will able to change this color at runtime.

4 Answers 4

20

Below code will work smoothly without slowing down check and uncheck behavior of checkbox.because checkbox.setSupportButtonTintList(colorStateList); will behave unexpectedly on some devices

ColorStateList  colorStateList = new ColorStateList(
                        new int[][]{
                                new int[]{-android.R.attr.state_checked}, // unchecked
                                new int[]{android.R.attr.state_checked} , // checked
                        },
                        new int[]{
                                Color.parseColor("#cccccc"),
                                Color.parseColor("##cccccc"),
                        }
                );

 CompoundButtonCompat.setButtonTintList(checkBox,colorStateList)
1
  • I just encountered the "unexpected behavior" you mention, and this code saved the day! Thanks a bunch for sharing! Nov 4, 2016 at 17:55
10

Use AppcompatCheckbox

 AppCompatCheckBox acb = (AppCompatCheckBox)findViewById(R.id.acb);
 ColorStateList colorStateList = new ColorStateList(
                new int[][]{

                     new int[]{-android.R.attr.state_enabled}, //disabled
                     new int[]{android.R.attr.state_enabled} //enabled
                },
                new int[] {

                     Color.RED //disabled
                     ,Color.BLUE //enabled

                }
        );

  acb.setSupportButtonTintList(colorStateList);
4
  • With this solution, click is slower than usual, but works though!
    – Valeriya
    Jul 29, 2016 at 13:46
  • try to extend AppCompatCheckBox, Add colorstate code; then see the performance @Lera Jul 30, 2016 at 10:04
  • I'm not too in touch with Android anymore, can some of you confirm if this answer fix the problem pls? Aug 15, 2016 at 11:04
  • I hope this solves the problem @MarceloFilho as i mentioned earlier you can also extend class from AppCompatCheckbox for performance Aug 17, 2016 at 6:41
7

This works for me:

public void setCheckBoxColor(CheckBox checkBox, int checkedColor, int uncheckedColor) {
     int states[][] = {{android.R.attr.state_checked}, {}};
     int colors[] = {checkedColor, uncheckedColor};
     CompoundButtonCompat.setButtonTintList(checkBox, new 
         ColorStateList(states, colors));
}
1
1

For an API level greater than or equal to Lollipop (21), try the below:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    checkBox.buttonTintList = ColorStateList.valueOf(ContextCompat.getColor(context, R.color.color_rose))
}
1
  • @PeterMortensen FYI it would probably be better to link to something with the numerical API level (here, 21) such as developer.android.com/reference/android/os/…, rather than the Wikipedia article. (flag as NLN once you've seen this)
    – Ryan M
    Aug 8, 2022 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.