Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Android CheckBox box's color programmatically (support library)?

I'm trying to change the checkbox box's color programmatically to a different color than the theme's default. The problem is I was doing something like this:

checkbox.setSupportButtonTintList(ColorStateList);

It works but it seems, according its class documentation, this method has been restricted to be used only by classes from the same package (com.android.support). This is the warning I got from Android Studio:

AppCompatCheckBox.setSupportButtonTintList can only be called from within the same library group (groupId=com.android.support)

Is there a standard/correct way of doing this for all API levels?

like image 930
rylexr Avatar asked Jan 19 '17 21:01

rylexr


People also ask

How can I change the color of my CheckBox in Android?

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.

Can we apply theme on CheckBox?

If you want to style particular checkbox then create your own style and extend it to @style/Widget. AppCompat. CompoundButton. CheckBox .

What is check box in Android Studio?

Checkboxes allow the user to select one or more options from a set. Typically, you should present each checkbox option in a vertical list. To create each checkbox option, create a CheckBox in your layout.

How do I toggle CheckBox in Android?

By default, the android CheckBox will be in the OFF (Unchecked) state. We can change the default state of CheckBox by using android:checked attribute. In case, if we want to change the state of CheckBox to ON (Checked), then we need to set android:checked = “true” in our XML layout file.


1 Answers

Based on rylexr answer, you can specify the color in the following way:

CompoundButtonCompat.setButtonTintList(checkboxView, ColorStateList
                        .valueOf(getResources().getColor(R.color.red)));
like image 57
Andrii Artamonov Avatar answered Oct 13 '22 14:10

Andrii Artamonov