Reputation: 698
I'm using the CheckBox view in Android. I would like to change the color of it when its checked. Right now its that default dark green color when its checked and I would like to change it to something different and when not checked, just be the default colors.
Here's my code:
CheckBox c = new CheckBox(this);
c.setId(View.generateViewId());
c.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(buttonView.isChecked())
{
buttonView.setBackgroundColor(Color.rgb(64, 131, 207));
}
if(!buttonView.isChecked())
{
buttonView.setBackgroundColor(Color.WHITE);
}
}
});
The problem is that it does not change the right thing. Any ideas on how to change this color?
Upvotes: 15
Views: 34418
Reputation: 598
It works well for me (kotlin)*
if (isChecked){
checkBox.buttonTintList = ColorStateList.valueOf(WHITE)
}else{
checkBox.buttonTintList = ColorStateList.valueOf(RED)
}
Upvotes: 0
Reputation: 1856
This is kotlin extension
fun AppCompatCheckBox.setTintAuto(enabledColor : Int, disabledColor : Int) {
val colorTint = ColorStateList(
arrayOf(
intArrayOf(-R.attr.state_checked),
intArrayOf(R.attr.state_checked)
), intArrayOf(enabledColor , disabledColor )
)
this.buttonTintList = colorTint
this.setTextColor(colorTint )
}
Then use it
checkBox.setTintAuto(activeColor,disabledColor)
Upvotes: 0
Reputation: 679
In kotlin you can just write this:
refBtn.buttonTintList = ColorStateList.valueOf(Color.GRAY)
You change change 'Color.GRAY' by the color you want, or write Color.rgb() and define the color :)
Upvotes: 3
Reputation: 6161
To tint the CompoundButton Tints try this, for Both the API>21 and below.
if (Build.VERSION.SDK_INT < 21) {
CompoundButtonCompat.setButtonTintList(button, ColorStateList.valueOf(tintColor));//Use android.support.v4.widget.CompoundButtonCompat when necessary else
} else {
button.setButtonTintList(ColorStateList.valueOf(tintColor));//setButtonTintList is accessible directly on API>19
}
Upvotes: 40
Reputation: 11695
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);
}
Upvotes: 24
Reputation: 93
implement this file in res
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_focused="true"
android:drawable="@drawable/checkbox_on_background_focus_yellow" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="@drawable/checkbox_off_background_focus_yellow" />
<item android:state_checked="false"
android:drawable="@drawable/checkbox_off_background" />
<item android:state_checked="true"
android:drawable="@drawable/checkbox_on_background" />
</selector>
and then add button to checkbox
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="new checkbox"
android:background="@drawable/checkbox_background"
android:button="@drawable/checkbox" />
Upvotes: 0
Reputation: 2985
Did you try to create a selector
and assign this selector
to your CheckBox
like this for example:
//drawable file called cb_selector
<?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/myCheckBox"
android:text="My CheckBox"
android:button="@drawable/cb_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
@drawable/checked
and @drawable/unchecked
are two images of your checkbox, so you could put there a color that you want
OR without changing the button layout, with adding this attribute to your checkbox
android:buttonTint="@color/YOUR_CHECKMARK_COLOR_HERE"
Upvotes: -4