Android: Change the color of RadioButtons and checkboxes programmatically

前端 未结 6 1208
轮回少年
轮回少年 2020-12-15 08:11

I created RadioButton and CheckBox in LinearLayout programatically. But, now I want to change radio button\'s color and check boxes\'s

6条回答
  •  感情败类
    2020-12-15 08:53

    For CheckBox, replace your CheckBox with AppCompatCheckBox and call following method:

    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);
    }
    

    I think it should be similar for RadioButton. You can check declared attributes in android.R.attr and change the code.

提交回复
热议问题