0

How to apply tint to CheckBox preference programmatically in Android?

CheckBox preference is already shown with grey because of the theme but I need to apply/change tint programmatically to some other colour. How to achieve this?

Georgios
  • 4,764
  • 35
  • 48

2 Answers2

0

Tinting Checkbox on pre v21

A checkbox can be tinted by using the theme atttribute and setting the colorControlNormal and colorControlActivated:

styles.xml

<style name="MyCheckBox" parent="Theme.AppCompat.Light">  
<item name="colorControlNormal">@color/indigo</item>
<item name="colorControlActivated">@color/pink</item>
</style> 

layout xml:

<CheckBox  
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Check Box"
        android:theme="@style/MyCheckBox"/>
Raluca
  • 41
  • 5
0

I suggest @ywwynm answer from How to change checkbox checked color programmatically

public static void setCheckBoxColor(AppCompatCheckBox checkBox, int uncheckedColor, int checkedColor) {
    ColorStateList colorStateList = new ColorStateList(
            new int[][] {
                    new int[] { -android.R.attr.state_checked }, // unchecked
                    new int[] {  android.R.attr.state_checked }  // checked
            },
            new int[] {
                    uncheckedColor,
                    checkedColor
            }
    );
    checkBox.setSupportButtonTintList(colorStateList);
}

setting setSupportButtonTintList with ColorStateList then setting checked and unchecked state programmatically will surely reach your answer