Android: Change the color of RadioButtons and checkboxes programmatically
Asked Answered
G

8

18

I created RadioButton and CheckBox in LinearLayout programatically. But, now I want to change radio button's color and check boxes's color. I use

RadioButton.setHighlightColor(Color.parseColor("#0c83bd"));

checkbox.setHighlightColor(Color.parseColor("#0c83bd"));

But it didn't work.

Gustafsson answered 9/9, 2015 at 6:53 Comment(3)
use background imagesWardwarde
but i want to set color not to attach imagesGustafsson
is there any function available in android support library?Wardwarde
R
14

Try this

AppCompatRadioButton newRadioButton = new AppCompatRadioButton(this);
AppCompatCheckBox newCheckBox = new AppCompatCheckBox(this);

Insted of

RadioGroup newRadioButton = new RadioGroup(this);
CheckBox newCheckBox = new CheckBox(this);
Roana answered 9/9, 2015 at 7:6 Comment(0)
I
22

Use AppCompatCheckBox and AppCompatRadioButton instead of CheckBox and RadioButton. Your xml will have :

<android.support.v7.widget.AppCompatCheckBox
    android:id="@+id/cbSelected"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgroundTint="@color/colorAccent" //This to set your default button color
    android:checked="true"/>

<android.support.v7.widget.AppCompatRadioButton
    android:id="@+id/rb"
    app:buttonTint="@color/colorAccent" //This to set your default button color
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

Now for the java code : Create ColorStateList

        ColorStateList colorStateList = new ColorStateList(
                new int[][]{
                        new int[]{android.R.attr.state_enabled} //enabled
                },
                new int[] {getResources().getColor(R.color.colorPrimary) }
        );

To change the color programatically for AppCompatRadioButton or AppCompatCheckBox call setSupportButtonTintList.

AppCompatRadioButton radioButton = (AppCompatRadioButton) findViewById(R.id.rb);
radioButton.setSupportButtonTintList(colorStateList);

AppCompatCheckBox cbSelected = (AppCompatCheckBox) findViewById(R.id.cbSelected);
cbSelected.setSupportButtonTintList(colorStateList);
Inelegancy answered 25/7, 2016 at 10:25 Comment(0)
O
16

You can create dynamic RadioButton like this:

RadioButton male = new RadioButton(ReservationContact.this);
male.setTextColor(Color.BLACK);
male.setText("Male");
male.setSelected(true);
male.setId(0);

This is used for changing default color of circle of RadioButton.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    male.setButtonTintList(ColorStateList.valueOf(ContextCompat.getColor(ReservationContact.this, R.color.background)));
}
male.setHighlightColor(getResources().getColor(R.color.background));
Ossiferous answered 28/4, 2017 at 12:47 Comment(0)
R
14

Try this

AppCompatRadioButton newRadioButton = new AppCompatRadioButton(this);
AppCompatCheckBox newCheckBox = new AppCompatCheckBox(this);

Insted of

RadioGroup newRadioButton = new RadioGroup(this);
CheckBox newCheckBox = new CheckBox(this);
Roana answered 9/9, 2015 at 7:6 Comment(0)
M
7

For CheckBox, 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);
}

I think it should be similar for RadioButton. You can check declared attributes in android.R.attr and change the code.

Matriculation answered 13/10, 2016 at 9:15 Comment(0)
W
1

I've tried a couple of ways to achieve it programmatically but none worked for me except following. Dropping it here, in case someone finds it helpful.

<androidx.appcompat.widget.AppCompatRadioButton
    android:id="@+id/radio_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@null" />

Create a ColorStateList

private fun getRadioButtonColor(): ColorStateList {
    val states = arrayOf(
            intArrayOf(-android.R.attr.state_checked),
            intArrayOf(android.R.attr.state_checked))

    val colors = intArrayOf(
            ContextCompat.getColor(context, R.color.grey2),
            ContextCompat.getColor(context, R.color.actionBlue)
    )

    return ColorStateList(states, colors)
}

And then use setButtonTintList from CompoutButtonCompat class to set this colorStateList

CompoundButtonCompat.setButtonTintList(radio_btn, getRadioButtonColor())
Wickham answered 14/5, 2021 at 16:52 Comment(0)
H
0

I am continuing the answer of ywwynm.

Google made the setSupportButtonTintList restricted so it is not possible to use it.

The workaround is to use the button as a TintableCompoundButton interface, in which the method is not restricted.

Works in API 19+ for AppCompatRadioButton. AppCompatCheckbox implements the same interface so it should work as well in theory but I haven't tested it.

Have fun :)

public static void setAppCompatRadioButtonColor(AppCompatRadioButton radioButton, 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
            }
    );
    ((TintableCompoundButton) radioButton).setSupportButtonTintList(colorStateList);
}
Hiltonhilum answered 29/11, 2018 at 15:39 Comment(0)
K
0

Try to add this line:

app:buttonTint="@color/yellow"

Example in your xml:

 <RadioButton
      android:id="@+id/radio"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      app:buttonTint="@color/yellow"
      android:text=" Radio"
      android:textColor="@color/yellow" />
Kristiekristien answered 28/9, 2021 at 13:32 Comment(0)
A
-1
   RadioButton rb=(RadioButton) findViewById(R.id.radioButton1);
    rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
            rb.setButtonDrawable(R.drawable.image);
            rb.setHighlightColor(Color.parseColor("#0c83bd"));


        }
    });
  }
Aideaidedecamp answered 9/9, 2015 at 6:59 Comment(1)
What if the user sets its own OnCheckedChangeListener?Stheno

© 2022 - 2024 — McMap. All rights reserved.