Android: Set Color of Checkbox

How to change the color of a CheckBox?

If your minSdkVersion is 21+ use android:buttonTint attribute to update the color of a checkbox:

<CheckBox
...
android:buttonTint="@color/tint_color" />

In projects that use AppCompat library and support Android versions below 21 you can use a compat version of the buttonTint attribute:

<CheckBox
...
app:buttonTint="@color/tint_color" />

In this case if you want to subclass a CheckBox don't forget to use AppCompatCheckBox instead.

PREVIOUS ANSWER:

You can change CheckBoxs drawable using android:button="@drawable/your_check_drawable" attribute.

How to change the check box (tick box) color to white in android XML

Set the colorAccent to your desired color:

<style name="AppTheme" parent="Theme.AppCompat">
<item name="colorAccent">#fff</item>
</style>

Or if you don't want to change your main theme, create a new theme and apply it only to the checkbox:

<style name="WhiteCheck">
<item name="colorAccent">#fff</item>
</style>

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/WhiteCheck"/>

Android: Set color of CheckBox

You can use a custom check box xml file for this. Save the below xml code in drawables folder, name it 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/cbchk_blue"
android:state_focused="false">
</item>
<item android:state_checked="true"
android:drawable="@drawable/cbchk_blue"
android:state_focused="true">
</item>
<item android:state_checked="false"
android:drawable="@drawable/cbunchk_blue"
android:state_focused="false">
</item>
<item android:state_checked="false"
android:drawable="@drawable/cbunchk_blue"
android:state_focused="true">
</item>
</selector>

Then use this file as background of your checkbox like this:

       <CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/custom_checkbox"
android:id="@+id/checkBox" />

Here I am uploading my own images which I used in place of cbchk_blue and cbunchk_blue

Unchecked CheckBox
Checked CheckBox

Checkbox color change android

The answer is very simple, check this code snippet below:

<CheckBox
android:id="@+id/checkBox1"
android:background="@drawable/tutorial_screen_edit_text_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckboxClicked"
android:text="Press to Skip"
android:buttonTint="@color/white" <!--just add this line-->
android:textColor="@color/white"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

How to change check box tick color in android

Unfortunately, changing the color of checkbox check mark isn't a simple attribute

Create a selector xml file in res\drawables\ folder with name cb_selector.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>

In your layout file apply this file to your checkBox

<CheckBox
android:id="@+id/cb"
android:text="My CheckBox"
android:button="@drawable/cb_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

Add a unchecked.png, and checked.png in your drawables folder. These are checked and unchecked image of checkbox.

Set checkbox color with a color selector in Android

I found a solution, based on some of the code posted by rafsanahmad007. I have created a custom checkbox view with a listener, changing the tint color based on its own state (checked or not).

Code for my custom view:

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.CompoundButtonCompat;
import android.util.AttributeSet;
import android.widget.CheckBox;
import android.widget.CompoundButton;

public class CustomCheckBox extends CheckBox implements CompoundButton.OnCheckedChangeListener {

private OnCheckedChangeListener listener;
private int currentColor;

public CustomCheckBox(Context context, AttributeSet attrs) {
super(context, attrs);
super.setOnCheckedChangeListener(this);
setCheckboxColor();
}

private void setCheckboxColor() {
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_enabled},
},
new int[]{
getCurrentColor()
}
);
CompoundButtonCompat.setButtonTintList(this, colorStateList);
}

@Override
public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
this.listener = listener;
}

public int getCurrentColor() {
if (isChecked()) {
return ContextCompat.getColor(getContext(), R.color.checkbox_checked);
} else {
return ContextCompat.getColor(getContext(), R.color.checkbox_unchecked);
}
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
setCheckboxColor();
if (listener != null) {
listener.onCheckedChanged(buttonView, isChecked);
}
}
}

Change Checkbox color only in checked state and disable user interaction

You can override the color in the single checkbox using:

AppCompat theme:

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LABEL"
android:checked="true"
android:theme="@style/OverlayCheckbox"/>

with:

<style name="OverlayCheckbox">
<item name="colorAccent">@color/light</item>
</style>

With a Material Components Theme:

<com.google.android.material.checkbox.MaterialCheckBox
...
android:theme="@style/ThemeOverlay.CheckBox"/>

with:

  <style name="ThemeOverlay.CheckBox" parent="">
<item name="colorSecondary">@color/....</item> <!-- checked -->
<item name="colorOnSurface">@color/.....</item> <!-- unchecked -->
</style>

With the Material Components Theme pay attention if you have defined the colorAccent in your app theme (you shouldn't add it in non-bridged Material Components theme).

In this case (I don't know if it is a bug or if it is an expected behavior) the MaterialCheckbox uses the colorAccent instead of the colorSecondary. Just add the colorAccent or colorControlActivated in the ThemeOverlay.CheckBox.



Related Topics



Leave a reply



Submit