VOOZH about

URL: https://www.geeksforgeeks.org/kotlin/radiogroup-in-kotlin/

⇱ RadioGroup in Kotlin - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

RadioGroup in Kotlin

Last Updated : 15 Jul, 2025

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

kotlin.Any
↳android.view.View
↳ android.view.ViewGroup
↳ android.widget.LinearLayout
↳ android.widget.RadioGroup

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

Example

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

Step by Step Implementation of RadioGroup

Step 1: Create new project

  • Click on File, then New => New Project.
  • Choose "Empty Activity" for the project template.
  • Select language as Kotlin.
  • Select the minimum SDK (According to the application needs).

Step 2: 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.

activity_main.xml:


Step 3: 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.

MainActivity.kt:

Output


Comment
Article Tags:

Explore