My Checkbox's background is specified via drawable xml and I want to change the color of these items via code. Different control in Android seems to have different ways to set its color.
<Checkbox android:button="@drawable/custom_checkbox" />
In drawable/custom_checkbox.xml:
<?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>
Most answers on SO stop at the solution above. I am able to change the color via the following code but this code will not work in certain API level (e.g. level 17). I would like something that works across the board.
Drawable d = DrawableCompat.wrap(checkbox.getBackground());
DrawableCompat.setTint(d, newColor);