How to change the color of the entire Сheckbox from what is default to Сheckbox ? That is, the Сheckbox and the text next to it should be the same color.

I know how to change the color of the text, but I did not find it so that I could find a box with a flag to change its color. Tried through styles, but also without result. It is necessary that the color of the unchecked Сheckbox one, and when clicked, it changes to another.

    3 answers 3

    You can create your own drawable, and then add in the checkbox

    android:drawableStart="@drawable/your_check_drawable" android:button="@android:color/transparent"

    Or create an owl selector and with 2 options drawable for different states and in drawableStart (for example) then add selector.

    https://stackoverflow.com/questions/5854047/how-to-change-the-color-of-a-checkbox

    • and where I prescribe color in drawable as far as I can see from examples, the color is not indicated anywhere, even if we then connect it to style.xml - Andrew Goroshko

    use AppCompatCheckBox and AppCompatRadioButton instead of CheckBox and RadioButton.

     <android.support.v7.widget.AppCompatCheckBox android:id="@+id/cbSelected" android:layout_width="wrap_content" android:layout_height="wrap_content" android:backgroundTint="@color/colorAccent" //This to set your default button color android:checked="true"/> <android.support.v7.widget.AppCompatRadioButton android:id="@+id/rb" app:buttonTint="@color/colorAccent" //This to set your default button color android:layout_width="wrap_content" android:layout_height="wrap_content"/> 

    Java code: Create a ColorStateList

      ColorStateList colorStateList = new ColorStateList( new int[][]{ new int[]{android.R.attr.state_enabled} //enabled }, new int[] {getResources().getColor(R.color.colorPrimary) } ); 

    Programmatically change the color for AppCompatRadioButton or AppCompatCheckBox by calling setSupportButtonTintList.

     AppCompatRadioButton radioButton = (AppCompatRadioButton) findViewById(R.id.rb); radioButton.setSupportButtonTintList(colorStateList); AppCompatCheckBox cbSelected = (AppCompatCheckBox) findViewById(R.id.cbSelected); cbSelected.setSupportButtonTintList(colorStateList); 

    A source

      redefine colors for checkbox state:

       Checkbox checkbox = findViewById(R.id.checkbox); int states[][] = {{android.R.attr.state_checked}, {}}; int colors[] = {R.color.color_checked, R.color.color_unchecked}; CompoundButtonCompat.setButtonTintList(checkbox, new ColorStateList(states, colors));