Skip to content
Related Articles

Related Articles

Improve Article

RadioGroup in Kotlin

  • Difficulty Level : Medium
  • Last Updated : 19 Feb, 2021

RadioGroup class of Kotlin programming language is used to create a container which holds multiple RadioButtons. The RadioGroup class is beneficial for placing a set of radio buttons inside it because this class adds multiple-exclusion scope feature to the radio buttons. This feature assures that the user will be able to check only one of the radio buttons among all which belongs to a RadioGroup class. If the user checks another radio button, the RadioGroup class unchecks the previously checked radio button. This feature is very important when the developer wants to have only one answer to be selected such as asking the gender of a user.

The class hierarchy of the RadioGroup class in Kotlin

Class Heirarchy of RadioGroup in Kotlin

XML attributes of RadioGroup widget

XML AttributesDescription
android:idTo uniquely identify the RadioGroup
android:backgroundTo set a background colour
android:onClickA method to perform certain action when RadioGroup is clicked
android:onClickIt’s a name of the method to invoke when the radio button clicked.
android:visibilityUsed to control the visibility i.e., visible, invisible or gone
android:layout_widthTo set the width
android:layout_heightTo set the height
android:contentDescriptionTo give a brief description of the view
android:checkedButtonStores id of child radio button that needs to be checked by default within this radio group
android:orientationTo fix the orientation constant of the view


In this example step by step demonstration of creating a RadioGroup will be covered, it consists of 5 RadioButton children which ask the user to choose a course offered by GeeksforGeeks. When the user checks one of the RadioButtons and clicks the Submit button a toast message appears which displays the selected option.

Note: Following steps are performed on Android Studio version 4.0

Create new project

  1. Click on File, then New => New Project.
  2. Choose “Empty Activity” for the project template.
  3. Select language as Kotlin.
  4. Select the minimum SDK as per your need.

Open activity_main.xml file

Below is the code for activity_main.xml file to add a RadioGroup and its 5 RadioButton children. A normal submit button is also added to accept and display the response selected by the user.

<?xml version="1.0" encoding="utf-8"?>
        app:layout_constraintVertical_bias="0.19" />
            android:textSize="24sp" />
            android:textSize="24sp" />
            android:textSize="24sp" />
            android:textSize="24sp" />
            android:textSize="24sp" />
        app:layout_constraintVertical_bias="0.17000002" />

Open MainActivity.kt file

Below is the code for MainActivity.kt file to access RadioGroup widget in kotlin file and show a proper message whenever a radio button is selected by the user.

package com.example.sampleproject
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    var radioGroup: RadioGroup? = null
    lateinit var radioButton: RadioButton
    private lateinit var button: Button
    override fun onCreate(savedInstanceState: Bundle?) {
        // Display name of the Application
        title = "RadioGroup in Kotlin"
        // Assigning id of RadioGroup
        radioGroup = findViewById(
        // Assigning id of Submit button
        button = findViewById(
        // Actions to be performed 
        // when Submit button is clicked
        button.setOnClickListener {
            // Getting the checked radio button id 
            // from the radio group
            val selectedOption: Int = radioGroup!!.checkedRadioButtonId
            // Assigning id of the checked radio button
            radioButton = findViewById(selectedOption)
            // Displaying text of the checked radio button in the form of toast
            Toast.makeText(baseContext, radioButton.text, Toast.LENGTH_SHORT).show()

Modify strings.xml file

All the strings which are used in the activity, from the application name to the option of various RadioButtons are listed in this file.

    <string name="app_name">RadioGroup in Kotlin</string>
    <string name="Heading">Choose a GfG course</string>
    <string name="RadioButton1">Amazon SDE Test Series</string>
    <string name="radioButton2">Placement 100</string>
    <string name="radioButton3">C++ STL</string>
    <string name="radioButton4">CS Core Subjects</string>
    <string name="radioButton5">DSA Self paced</string>
    <string name="submit_Button">Submit</string>


RadioGroup Activity
RadioGroup Activity
Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

My Personal Notes arrow_drop_up
Recommended Articles
Page :