As of now checkbox is filled with the toolbar theme color but I need to show checkbox with different color other than toolbar so I need to update checkbox color programmatically in android. How to do the same?
Asked
Active
Viewed 279 times
2 Answers
1
For Checkboxpreference you should use this:
https://stackoverflow.com/a/6042324/11549280
and
for CheckBox you should use: Suppose your checkbox is:
Checkbox checkbox;
To create a ColorStateList programmatically,
Use
ContextCompat.getColorStateList(context, R.color.your_color);
and then
CompoundButtonCompat.setButtonTintList(checkbox, colorStateList);

Yogesh Nikam
- 349
- 1
- 2
- 11

Yogesh Nikam Patil
- 1,192
- 13
- 18
-
hi,it is CheckBoxPreference not checbox CheckBoxPreference itself will give you check box in build which cannot be accessed.Plz check – Hemavathi Nov 27 '19 at 07:24
0
Create theme to your setting activity, add this parameters to the style:
<style name="AppTheme.NoActionBar.SettingActivity" parent="AppTheme.NoActionBar">
<!--when the check box is checked --!>
<item name="colorControlNormal">@color/your-color</item>
<!--when the check box is unchecked --!>
<item name="colorControlActivated">@color/your-color</item>
</style>`
if you are using AppCompatCheckBox follow the instruction below
Use buttonTint to change color of button and color selector for above 21+ api version.
<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="@color/checkbox_filter_tint"
tools:targetApi="21"/>
res/colors/checkbox_filter_tint.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/light_gray_checkbox"
android:state_checked="false"/>
<item android:color="@color/common_red"
android:state_checked="true"/>
</selector>

Vinay Jayaram
- 1,030
- 9
- 29