Open In App

RadioButton in Kotlin

Last Updated : 19 Feb, 2021
Like Article

Android Radio Button is bi-state button which can either be checked or unchecked. Also, it’s working is same as Checkbox except that radio button can not allow to be unchecked once it was selected.

Generally, we use RadioButton controls to allow users to select one option from multiple options.

By default, the RadioButton in OFF(Unchecked) state but we can change the default state of RadioButton by using android:checked attribute.

Following steps to create new project-

  • Click on File,then New => New Project.
  • Then, check Include Kotlin Support and click next button.
  • Select minimum SDK, whatever you need.
  • Select Empty activity and then click finish.

Different attributes of RadioButton widget

XML Attributes Description
android:id Used to uniquely identify the control
android:gravity Used to specify how to align the text like left, right, center, top, etc.
android:checked Used to specify the current state of radio button
android:onClick It’s a name of the method to invoke when the radio button clicked.
android:textSize Used to set size of the text.
android:textColor Used to set color of the text.
android:textStyle Used to set style of the text. For example, bold, italic, bolditalic etc.
android:maxWidth Used to make the view be at most this many pixels wide.
android:minWidth Used to make the view be at least this many pixels wide.
android:background Used to set the background of the radio button control.
android:visibility Used to control the visibility.

Modify the strings.xml file

We can write the name of the application as RadioButtonInKotlin and write other strings which can be used.

    <string name="app_name">RadioButtonInKotlin</string>
    <string name="checked">checked</string>
    <string name="unchecked">unchecked</string>

Add RadioButtons in activity_main.xml file

In android, we use radio buttons inside RadioGroup to combine set of radio buttons into single group and it will make sure that user can select only button from the group of buttons.

<?xml version="1.0" encoding="utf-8"?>
            android:text="Which is your favorite color?"
        android:text="Get Selected Color"/>

Here, we are trying to implement a scenario where you need to select your favorite color. So, in activity_main.xml file, we have added 4 radio buttons inside a radio group. Each button represent a color. Now, only one radio button can be selected at a time. *

Now, we will access this widget in kotlin file and show a proper message whenever a radio button is selected.

Now, Open MainActivity.kt file and add below code into it.

package com.geeksforgeeks.myfirstkotlinapp
import android.os.Bundle
import android.view.View
import android.widget.*
import android.widget.RadioGroup
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Get radio group selected item using on checked change listener
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                Toast.makeText(applicationContext," On checked change :"+
                        " ${radio.text}",
        // Get radio group selected status and text using button click event
            // Get the checked radio button id from radio group
            var id: Int = radio_group.checkedRadioButtonId
            if (id!=-1){ // If any radio button checked from radio group
                // Get the instance of radio button using id
                val radio:RadioButton = findViewById(id)
                Toast.makeText(applicationContext,"On button click :" +
                        " ${radio.text}",
                // If no radio button checked in this radio group
                Toast.makeText(applicationContext,"On button click :" +
                        " nothing selected",
    // Get the selected radio button text using radio button on click listener
    fun radio_button_click(view: View){
        // Get the clicked radio button instance
        val radio: RadioButton = findViewById(radio_group.checkedRadioButtonId)
        Toast.makeText(applicationContext,"On click : ${radio.text}",

In MainActivity.kt file, we have accessed radio group in which I have added four radio buttons. Then, we have set a listener to display toast message whenever radio button selection changes.

Since AndroidManifest.xml file is very important in any android application, we are also going to mention it here.

AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

When You run the application, you will get the output as shown below

Run as Emulator:

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads