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
XML attributes of RadioGroup widget
|android:id||To uniquely identify the RadioGroup|
|android:background||To set a background colour|
|android:onClick||A method to perform certain action when RadioGroup is clicked|
|android:onClick||It’s a name of the method to invoke when the radio button clicked.|
|android:visibility||Used to control the visibility i.e., visible, invisible or gone|
|android:layout_width||To set the width|
|android:layout_height||To set the height|
|android:contentDescription||To give a brief description of the view|
|android:checkedButton||Stores id of child radio button that needs to be checked by default within this radio group|
|android:orientation||To 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
- Click on File, then New => New Project.
- Choose “Empty Activity” for the project template.
- Select language as Kotlin.
- 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.
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.
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.
- Dynamic RadioGroup in Kotlin
- Kotlin Set : setOf()
- Kotlin | apply vs with
- Kotlin hashSetOf()
- Kotlin Hashmap
- Kotlin Map : mapOf()
- Kotlin Ranges
- Triple in Kotlin
- Pair in Kotlin
- Kotlin Reflection
- Kotlin Array
- Kotlin String
- HorizontalScrollView in Kotlin
- Kotlin Collections
- Comparator in Kotlin
- RatingBar in Kotlin
- Chronometer in Kotlin
- SeekBar in Kotlin
- CheckedTextView in Kotlin
- Switch in Kotlin
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.