Skip to content Skip to sidebar Skip to footer

How To Programatically Set The Colour Of A Radiobutton Android Circle

I want to change the actual circle of the RadioButton. I have looked all over StackOverflow and nothing seems to work. I am using API 17 and cannot use a ColorStateList. Custom dra

Solution 1:

How to programatically set the color of a RadioButton Android

That is really non-specific, so here are the most relevant I found:

RadioButton rad;//initialize first!//You can set the background color
rad.setBackgroundColor(Color.BLUE);
//Text color
rad.setTextColor(Color.WHITE);
//or highlight color
rad.setHighlightColor(Color.GREEN);

The highlight color is the color that appears when you press and hold the RadioButton(default is yellow-ish)

EDIT:

In the initialization of the radio button, call it an AppCompatRadioButton instead

AppCompatRadioButton rad = ....
rad.setHighlightColor(Color.GREEN);

Reworked from: https://stackoverflow.com/a/32472971/6296561

EDIT

Try this:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"app:layout_behavior="@string/appbar_scrolling_view_behavior"tools:context=".MainActivity"tools:showIn="@layout/app_bar_main"><RadioGroupandroid:id="@+id/radioGroup"android:layout_width="wrap_content"android:layout_height="wrap_content"></RadioGroup></RelativeLayout>

MainActivity.java

publicclassMainActivityextendsAppCompatActivityimplementsNavigationView.OnNavigationItemSelectedListener {

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbartoolbar= (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        RadioGroupradioGroup= (RadioGroup) findViewById(R.id.radioGroup);

        /**
         * First Radio Button
         */
        RadioButton RB1= (RadioButton) getLayoutInflater().inflate(R.layout.custom_radiobutton, null);//initialize and set content
        RB1.setText("RB1");
        radioGroup.addView(RB1);//add the radiobutton to the radiogroup defined in the layout/**
         * Second Radio Button
         */RadioButtonRB2= (RadioButton) getLayoutInflater().inflate(R.layout.custom_radiobutton, null);//initialize and set content
        RB2.setText("RB2");
        radioGroup.addView(RB2);//add the radiobutton to the radiogroup defined in the layout
    }
}

custom_radiobutton.xml

<?xml version="1.0" encoding="utf-8"?><RadioButtonxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:buttonTint="@color/colorPrimary"android:text=""><!-- leave empty --></RadioButton>

NOTE:buttonTintonly works in API 21+. (Untested) you can change RadioButton to AppCompatRadioButton. (It is untested so I am not sure if it works on api 20 and lower)

<?xml version="1.0" encoding="utf-8"?><AppCompatRadioButtonxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:buttonTint="@color/colorPrimary"android:text=""><!-- leave empty --></AppCompatRadioButton>

Documentation about AppCompatRadioButton

If you use AppCompatRadioButton, I think you also have to use AppCompatRadioGroup and edit the creation of the ACRB's to:

AppCompatRadioButtonRB1= (AppCompatRadioButton) getLayoutInflater().inflate(R.layout.custom_radiobutton, null);//initialize and set content
RB1.setText("RB1");
radioGroup.addView(RB1);//add the radiobutton to the radiogroup defined in the layout

Solution 2:

Changing color of RadioButton circle can be done by using your own custom images. Lets say you have two circular images for checked and unchecked viz. checkedradiobutton and uncheckedradiobutton. Now Make a xml(custom_radio_button) drawable as below:

<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_checked="true"android:drawable="@drawable/checkedradiobutton" /><itemandroid:state_checked="false"android:drawable="@drawable/unchekedradiobutton" />

I believe your are creating your Radio button using

RadioButtonrb=newRadionButton(context);
  radioButton.setLayoutParams(newViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
  ViewGroup.LayoutParams.WRAP_CONTENT));
  radioButton.setTextColor(ContextCompat.
  getColor(context,R.color.royal_blue));

 radioButton.setButtonDrawable(R.drawable.custom_radio_button);//this will change the default circles

Post a Comment for "How To Programatically Set The Colour Of A Radiobutton Android Circle"