I'm creating an ordinary Checkbox view:
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
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!
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.
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.
You can create dynamic RadioButton like this: RadioButton male = new RadioButton(ReservationContact. this); male. setTextColor(Color.
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)
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With