How to change flag mark color programmatically

I am using CheckBox view in Android. I would like to change its color when it is checked. Right now, when it was marked by default with dark green, and I would like to change it to something else, and when it was not marked, just specify the default colors.

Here is 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?

enter image description here

+5
source share
4 answers

You tried to create a selector and assign this selector to your CheckBox as follows:

 //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 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 can add the color you want there

OR without changing the layout of the buttons by adding this attribute to your checkbox

 android:buttonTint="@color/YOUR_CHECKMARK_COLOR_HERE" 
-2
source

Replace CheckBox with AppCompatCheckBox and execute the 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); } 
+7
source

To tint CompoundButton Tints, try this for both APIs> 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 } 
+3
source
 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" /> 
0
source

Source: https://habr.com/ru/post/1238546/


All Articles