RadioGroup & RadioButton

RadioGroup & RadioButton 

Think of RadioButtons like options in a quiz or multiple-choice question, and RadioGroup as the container that makes sure only one option can be selected.

Example:

Select your favorite color:

⚪ Red  
⚪ Blue  
⚪ Green

Here:

  • Each ⚪ is a RadioButton

  • The whole group is a RadioGroup


1. Definitions

RadioButton

A RadioButton is a UI component in Android that allows the user to select one option from multiple choices.

Simple meaning:

RadioButton = A circular button representing one choice.

RadioGroup

A RadioGroup is a container that holds multiple RadioButtons and ensures that only one can be selected at a time.

Simple meaning:

RadioGroup = A parent container that manages RadioButtons.

2. Purpose

  • RadioButton: Let the user select one choice

  • RadioGroup: Ensure only one selection is possible

Example uses:

App FeatureExample
Gender selectionMale / Female / Other
Quiz appOption A / B / C / D
SettingsLow / Medium / High volume

3. Basic Syntax (XML)

Example: RadioGroup with 3 RadioButtons

<RadioGroup
    android:id="@+id/radioGroupColors"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RadioButton
        android:id="@+id/radioRed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Red"/>

    <RadioButton
        android:id="@+id/radioBlue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Blue"/>

    <RadioButton
        android:id="@+id/radioGreen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Green"/>
</RadioGroup>

Explanation:

  • RadioGroup → parent container

  • RadioButton → child options

  • Only one RadioButton can be selected in the group


4. Using RadioGroup & RadioButton in Kotlin

import android.os.Bundle
import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val radioGroup = findViewById<RadioGroup>(R.id.radioGroupColors)

        radioGroup.setOnCheckedChangeListener { group, checkedId ->
            val radioButton = findViewById<RadioButton>(checkedId)
            println("Selected color: ${radioButton.text}")
        }
    }
}

Explanation:

  • findViewById() connects Kotlin to XML

  • setOnCheckedChangeListener() detects which RadioButton is selected

  • checkedId → the id of selected RadioButton


5. Important Properties

RadioButton Properties

PropertyDescription
android:textText label of the option
android:checkedSets default selection (true or false)
android:enabledEnable or disable option
android:buttonTintColor of the circle button
android:gravityAligns text

RadioGroup Properties

PropertyDescription
android:orientationvertical or horizontal
android:checkedButtonID of default selected button
android:layout_widthWidth of the group
android:layout_heightHeight of the group

6. Important Methods (Kotlin)

RadioButton Methods

radioButton.isChecked         // Check if selected
radioButton.text              // Get label text
radioButton.isEnabled = false // Disable option

RadioGroup Methods

radioGroup.checkedRadioButtonId        // Get selected RadioButton id
radioGroup.clearCheck()                // Deselect all buttons
radioGroup.setOnCheckedChangeListener { group, checkedId -> ... } // Detect selection

7. Real-Time Examples

Example 1: Gender Selection

XML:

<RadioGroup
    android:id="@+id/radioGroupGender"
    android:orientation="horizontal">
    <RadioButton android:id="@+id/radioMale" android:text="Male"/>
    <RadioButton android:id="@+id/radioFemale" android:text="Female"/>
    <RadioButton android:id="@+id/radioOther" android:text="Other"/>
</RadioGroup>

Kotlin:

val genderGroup = findViewById<RadioGroup>(R.id.radioGroupGender)
genderGroup.setOnCheckedChangeListener { group, checkedId ->
    val selected = findViewById<RadioButton>(checkedId)
    println("Selected gender: ${selected.text}")
}

Example 2: Quiz App Question

Q: What is 2 + 2?

⚪ 3  
⚪ 4  
⚪ 5

Only one option can be chosen using RadioGroup.


Example 3: Settings Option

Select Volume Level:

⚪ Low  
⚪ Medium  
⚪ High

Kotlin:

volumeGroup.setOnCheckedChangeListener { group, checkedId ->
    val level = findViewById<RadioButton>(checkedId)
    println("Volume set to ${level.text}")
}

8. Advantages

✔ Only one selection allowed → avoids confusion
✔ Easy to implement multiple-choice options
✔ Works well with forms, surveys, and settings


9. Disadvantages

❌ Cannot select multiple options (use CheckBox for that)
❌ Needs a RadioGroup to manage selection; standalone RadioButtons won’t enforce single selection


10. One-Line Summary

RadioButton = A circular option for users to choose one choice; RadioGroup = A container that manages multiple RadioButtons and allows only one selection.



Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form