In Android user interfaces, RadioButtons allow users to select a single choice from a group of mutually exclusive options. They are typically grouped using RadioGroup, which ensures that only one RadioButton can be selected at a time.
This guide explains how to set up RadioButtons, handle user selection, and access the selected value with practical examples.
What Is a RadioButton?
A RadioButton is a UI component that represents a selectable option. RadioButtons work in pairs or groups where only one option should be selected at a time.
A RadioGroup wraps multiple RadioButtons to enforce exclusive selection automatically.
When to Use RadioButton
Use RadioButtons when:
- There is a limited set of choices (e.g., gender, delivery options)
- Only one option should be selected
- You want clear visual feedback of the selected state
For multiple independent selections, use CheckBoxes instead.
Basic RadioButton in XML
Define a RadioGroup with multiple RadioButton elements in your layout (e.g., activity_main.xml):
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<RadioButton
android:id="@+id/rbOption1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1"/>
<RadioButton
android:id="@+id/rbOption2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2"/>
<RadioButton
android:id="@+id/rbOption3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 3"/>
</RadioGroup>
This layout displays three choices stacked vertically.
Handling Selection in Java
To listen for selection changes, set a OnCheckedChangeListener on the RadioGroup:
RadioGroup radioGroup = findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
RadioButton selectedRadio = findViewById(checkedId);
if (selectedRadio != null) {
String selectedText = selectedRadio.getText().toString();
Toast.makeText(this, "Selected: " + selectedText, Toast.LENGTH_SHORT).show();
}
});
Here:
checkedIdis the ID of the selected RadioButton- You can access text or tag associated with the selection
This listener triggers whenever the user selects a different option.
Accessing Selected Option Programmatically
If you need to read the selected RadioButton on a button click:
Button btnSubmit = findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(v -> {
int selectedId = radioGroup.getCheckedRadioButtonId();
if (selectedId != -1) {
RadioButton selectedRadio = findViewById(selectedId);
String value = selectedRadio.getText().toString();
// Handle the selected value
} else {
Toast.makeText(this, "Please select an option", Toast.LENGTH_SHORT).show();
}
});
This pattern is common when submitting a form.
Example: Gender Selection
To demonstrate a real use case:
<RadioGroup
android:id="@+id/genderGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rbMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"/>
<RadioButton
android:id="@+id/rbFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"/>
</RadioGroup>
In code:
RadioGroup genderGroup = findViewById(R.id.genderGroup);
genderGroup.setOnCheckedChangeListener((group, checkedId) -> {
RadioButton radio = findViewById(checkedId);
String gender = radio.getText().toString();
Log.d("Gender", gender);
});
RadioButton With Tags
Use tags if RadioButton text isn’t the data you need:
<RadioButton
android:id="@+id/rbYes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes"
android:tag="TRUE"/>
Access the tag value:
String tagValue = selectedRadio.getTag().toString();
Tags are useful when the displayed label differs from the logical value.
Styling RadioButtons
You can style RadioButtons using XML attributes or themes:
<RadioButton
style="@style/Widget.AppCompat.CompoundButton.RadioButton"
android:textColor="@color/primaryText"
android:buttonTint="@color/primaryColor"/>
Apply consistent styles across your application for a unified look and feel.
RadioGroup Attributes You Should Know
| Attribute | Description |
|---|---|
android:orientation | Vertical or horizontal layout |
android:checkedButton | ID of the initially selected button |
android:layoutDirection | LTR or RTL support |
For example:
android:checkedButton="@id/rbOption2"
preselects the second option.
Best Practices (Senior Engineering Insight)
From real Android application experience:
- Use
RadioGroupto enforce mutually exclusive selection - Always validate that a selection exists before processing data
- Avoid deep nested layouts around RadioGroups for performance
- Use
buttonTintwith state lists for consistent theming
These practices help build resilient and accessible forms.
Common Issues and Fixes
RadioButton not responding on click:
✔ Confirm the RadioGroup wraps the RadioButtons
No selection detected:
✔ Check for getCheckedRadioButtonId() != -1
Text not updating or binding issue:
✔ Ensure you call findViewById() after setContentView()
Summary
Android RadioButtons are essential for creating simple single-choice interfaces. Together with RadioGroup, they provide a convenient pattern for enforcing exclusive choices. By implementing listeners, accessing selected values, and using tags or styling, you can build integrated forms with clean user interactions.


