Java – how to programmatically change the color selected in the check box

I use it in Android Check@R_88_2419 @View I want to change its color when checking Now it is the default dark green. When it is checked, I want to change it to something different. When it is not checked, it is only the default color

This is my code:

Check@R_88_2419@ c = new Check@R_88_2419@(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 doesn't change the right thing Any ideas on how to change this color?

Solution

Do you want to try to create a selector and assign it to Check@R_88_2419 @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 the layout file, apply this file to check@R_88_2419 @

<Check@R_88_2419@
    android:id="@+id/myCheck@R_88_2419@"
    android:text="My Check@R_88_2419@"
    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 check box, so you can put a color you want

Or add this property to the check box without changing the button layout

android:buttonTint="@color/YOUR_checkmark_COLOR_HERE"
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>