Open In App

RadioGroup in Kotlin

Last Updated : 19 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 Attributes Description
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

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

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"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    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>


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 androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.Toast
import 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()
        }
    }
}


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.




<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>


Output

RadioGroup Activity

RadioGroup Activity


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads