RadioGroup in Kotlin

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.
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
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.
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.
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"?><androidx.constraintlayout.widget.ConstraintLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#168BC34A"    tools:context=".MainActivity">      <TextView        android:id="@+id/textView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:fontFamily="@font/roboto"        android:text="@string/Heading"        android:textAlignment="center"        android:textColor="@android:color/holo_green_dark"        android:textSize="36sp"        android:textStyle="bold"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent"        app:layout_constraintVertical_bias="0.19" />      <RadioGroup        android:id="@+id/radioGroup1"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:background="#024CAF50"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toBottomOf="@+id/textView"        app:layout_constraintVertical_bias="0.24000001">          <RadioButton            android:id="@+id/radioButton1"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:fontFamily="@font/roboto"            android:text="@string/RadioButton1"            android:textSize="24sp" />          <RadioButton            android:id="@+id/radioButton2"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:fontFamily="@font/roboto"            android:text="@string/radioButton2"            android:textSize="24sp" />          <RadioButton            android:id="@+id/radioButton3"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:fontFamily="@font/roboto"            android:text="@string/radioButton3"            android:textSize="24sp" />          <RadioButton            android:id="@+id/radioButton4"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:fontFamily="@font/roboto"            android:text="@string/radioButton4"            android:textSize="24sp" />          <RadioButton            android:id="@+id/radioButton5"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:fontFamily="@font/roboto"            android:text="@string/radioButton5"            android:textSize="24sp" />    </RadioGroup>      <Button        android:id="@+id/submitButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="#AB4CAF50"        android:text="@string/submit_Button"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toBottomOf="@+id/radioGroup1"        app:layout_constraintVertical_bias="0.17000002" /></androidx.constraintlayout.widget.ConstraintLayout>
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 androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.view.Viewimport android.widget.Buttonimport android.widget.RadioButtonimport android.widget.RadioGroupimport android.widget.Toastimport kotlinx.android.synthetic.main.activity_main.*  class MainActivity : AppCompatActivity() {    var radioGroup: RadioGroup? = null    lateinit var radioButton: RadioButton    private lateinit var button: Button      override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          // Display name of the Application        title = "RadioGroup in Kotlin"          // Assigning id of RadioGroup        radioGroup = findViewById(R.id.radioGroup1)          // Assigning id of Submit button        button = findViewById(R.id.submitButton)          // 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()        }    }}
All the strings which are used in the activity, from the application name to the option of various RadioButtons are listed in this file.
<resources>    <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></resources>
Android-Button
Kotlin Android
Picked
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Android SDK and it's Components
Android RecyclerView in Kotlin
Navigation Drawer in Android
Android Project folder Structure
Flutter - Custom Bottom Navigation Bar
Android RecyclerView in Kotlin
Android UI Layouts
Kotlin constructor
Retrofit with Kotlin Coroutine in Android
Content Providers in Android with Example