2

I have an Activity with some radio buttons and checboxes. ATM they look like this:

enter image description here

Now i want to change this greenish accent color to something else. How would be the best way to achive that?

randomdev8712
  • 261
  • 1
  • 6
  • 23

3 Answers3

1

You can use the OnCheckedChangeListener and one own image for this. For example:

checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {

            if (isChecked) {

                checkbox.setButtonDrawable(R.drawable.yourImage);
            }

        }
    });

Use a one-color-image when you want a one-color-background like the green one.

Patricia
  • 2,885
  • 2
  • 26
  • 32
  • How would i create this image? And isnt this a bit complex to do it for every checkbox? isnt there a way to change that for all checboxes generally? – randomdev8712 Oct 18 '15 at 10:47
1

create a checkbox_selector.xml in your drawable folder like this, and you need two image normal_checkbox.png and selected_checkbox.png

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/normal_checkbox" 
          android:state_checked="false"/>
    <item android:drawable="@drawable/selected_checkbox" 
          android:state_checked="true"/>
    <item android:drawable="@drawable/normal_checkbox"/>    
</selector>

and now change your layout file where you declare CheckBox

<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:button="@drawable/checkbox_selector"
android:text="My CheckBox"
android:textColor="@color/Black" />

for more click

Community
  • 1
  • 1
Tanim reja
  • 2,120
  • 1
  • 16
  • 23
0

For those looking for this in 2021 and after, now there's a simplier way to accomplish this, using just this little piece of css:

input[type=checkbox], input[type=radio] {
    accent-color: #FFCD00;
}
tinny77
  • 41
  • 4