Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Checkbox colorAccent in runtime programmatically

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 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 in runtime: How to set colorAccent in code?

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

Thanks!

like image 717
Idemax Avatar asked Mar 01 '15 23:03

Idemax


People also ask

How do I change the color of a checkbox?

If you want to change checkbox color then "colorAccent" attribute will use for checked state and "android:textColorSecondary" will use for unchecking state. "actionOverflowButtonStyle" will use for change the color of overflow icon in the Action bar. Same is for refresh button which i am using in my app.

How can I change checkbox background in Android?

This example demonstrates how do I change the color of the check box in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I change the selected radio button color in android programmatically?

You can create dynamic RadioButton like this: RadioButton male = new RadioButton(ReservationContact. this); male. setTextColor(Color.


2 Answers

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)
like image 109
Amol Suryawanshi Avatar answered Nov 09 '22 06:11

Amol Suryawanshi


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);
like image 43
Renjith Thankachan Avatar answered Nov 09 '22 05:11

Renjith Thankachan