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"?>
<RelativeLayout xmlns: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">
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioGroup>
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
RadioGroup radioGroup = (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
*/
RadioButton RB2 = (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"?>
<RadioButton xmlns: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: buttonTint
only 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"?>
<AppCompatRadioButton xmlns: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:
AppCompatRadioButton RB1 = (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