Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; SEOFriendlyImages has a deprecated constructor in /home/tutorig3/public_html/wp-content/plugins/seo-image/seo-friendly-images.class.php on line 2

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/tutorig3/public_html/wp-content/themes/mts_interactive/options/php-po/php-po.php on line 185
Android Radio Group Using Kotlin With Example - Tutorialwing

Android Radio Group Using Kotlin With Example

Hello Readers! In this post, we are going to learn about how to use android radio group using kotlin in any android application. We will also learn about different attributes of radio group that can be used to customise this widget.


Tutorialwing Android Radio Group Output Android Radio Group Using Kotlin

Tutorialwing Android Radio Group Output

Getting Started

Android Radio Group can be defined as below –

Radio Group is used to create a multiple-exclusion scope for a set of radio buttons i.e. You can select only one radio button out of all radio buttons present in any radio group. Selecting one radio button automatically de-select previously selected radio button.

Different Attributes of Android Radio Group Widget

Some of the popular attributes of Radio Group inherited from LinearLayout are –

Sr. XML Attributes Description
1 android:baselineAligned Set whether to align RadioGroup’s children baselines
2 android:divider Set drawable to be used as a vertical divider between buttons
3 android:gravity Defines how an object will be aligned within layout
4 android:orientation Specifies how the object should be aligned within this layout. For example, horizontal or vertical etc.
5 android:weightSum Specifies maximum weight sum

Some of the popular attributes of android Radio Group inherited from ViewGroup are –

Sr. XML Attributes Description
1 android:animationCache Specifies whether layout animations should create a drawing cache for their children
2 android:clipChildren Specifies whether a child is limited to draw inside of its bounds or not.
3 android:layoutMode Specifies the layout mode of this ViewGroup.

Some of the popular attributes of android Radio Group inherited from View are –

Sr. XML Attributes Description
1 android:alpha Specifies alpha of the view
2 android:background Specifies background of the view
3 android:backgroundTint Specifies background tint
4 android:clickable Specifies whether view is clickable or not
5 android:id Specifies id of the view. Note- Id must be unique within an xml file

Example of Android Radio Group Using Kotlin

At first, we will create android application. Then, we will use radio group using kotlin in the application.

1. Creating New Project in Kotlin

Follow steps below to create new project. Please ignore the steps if you have already created the project.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as RadioGroup. Then, check Include Kotlin Support and click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. You will get a newly created project successfully if you have followed steps properly.

Since we have a project now, we will modify xml and other files to use radio group using kotlin in the application.

2. Modify values folder

No values folders have been modified. So, we are not going to mention them here.

3. Use Radio Group Widget in xml file

Open src/main/res/layout/activity_main.xml file and add below code into it.

<?xml version="1.0" encoding="utf-8"?>






In activity_main.xml file, we have used radio group with two radio buttons. Using Radio Buttons inside Radio group makes them mutually exclusive selectable i.e. It makes radio buttons only one selectable at a time. Now, we will access radio group using kotlin file.

4. Access Radio Group Widget in Kotlin file

Open src/main/java/com.tutorialwing.radiogroup/MainActivity.kt file and add below code into it.

package com.tutorialwing.radiogroup

import android.os.Bundle
import android.widget.RadioGroup
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        val radioGroup = findViewById<RadioGroup>(
        radioGroup?.setOnCheckedChangeListener { group, checkedId ->
            var text = "You selected: "
            text += if ( == checkedId) "male" else "female"
            Toast.makeText(applicationContext, text, Toast.LENGTH_SHORT).show()

We have accessed radio group using kotlin file i.e. MainActivity.kt file. Then, we have set checked change listener to show toast message whenever there is change in radio button selections.

Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.

AndroidManifest.xml file

Code inside main/AndroidManifest.xml file is as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.radiogroup"

		<activity android:name=".MainActivity">
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>


When we run the program, we will get output as shown above.

That’s end of our tutorial on Android Radio Group using Kotlin.