How to Use Radio Button in Android with Kotlin
In Android Kotlin, use
RadioButton inside a RadioGroup to allow single selection. Handle user choice by setting a listener on the RadioGroup that detects which RadioButton is selected.Syntax
A RadioButton is a selectable button that allows the user to choose one option from a set. It must be placed inside a RadioGroup to ensure only one button is selected at a time.
The RadioGroup manages the selection and provides a listener to detect changes.
xml
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2" />
</RadioGroup>Output
UI with two radio buttons labeled 'Option 1' and 'Option 2' where only one can be selected at a time.
Example
This example shows how to set up a RadioGroup with two RadioButtons in XML and handle the selection in Kotlin code to display the chosen option.
kotlin
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val radioGroup = findViewById<RadioGroup>(R.id.radioGroup) radioGroup.setOnCheckedChangeListener { group, checkedId -> if (checkedId != -1) { val radioButton = findViewById<RadioButton>(checkedId) Toast.makeText(this, "Selected: ${radioButton.text}", Toast.LENGTH_SHORT).show() } } } }
Output
When user selects a radio button, a toast message shows the selected option text.
Common Pitfalls
- Not placing
RadioButtons inside aRadioGroupcauses multiple buttons to be selectable at once. - Forgetting to check if
checkedIdis not -1 before using it can cause crashes. - Using
setOnClickListeneron individualRadioButtons instead ofsetOnCheckedChangeListeneronRadioGroupcan lead to inconsistent state.
xml
/* Wrong: RadioButtons outside RadioGroup allow multiple selection */ <RadioButton android:id="@+id/radioButton1" android:text="Option 1" /> <RadioButton android:id="@+id/radioButton2" android:text="Option 2" /> /* Right: Place RadioButtons inside RadioGroup */ <RadioGroup> <RadioButton android:id="@+id/radioButton1" android:text="Option 1" /> <RadioButton android:id="@+id/radioButton2" android:text="Option 2" /> </RadioGroup>
Quick Reference
Tips for using RadioButton in Android Kotlin:
- Always group
RadioButtons inside aRadioGroup. - Use
setOnCheckedChangeListeneronRadioGroupto detect selection changes. - Access selected button with
findViewById<RadioButton>(checkedId). - Check if
checkedIdis not-1before using it.
Key Takeaways
Place RadioButtons inside a RadioGroup to allow single selection.
Use RadioGroup's setOnCheckedChangeListener to detect which RadioButton is selected.
Always check that the selected RadioButton ID is valid before accessing it.
Avoid setting click listeners on individual RadioButtons for selection handling.
Use Toast or other UI feedback to confirm user selection.