Android Radio Button Using Kotlin With Example

Hello Readers! In this post, we will learn about android radio button using kotlin. We will see different attributes of radio button that can be used to customise this widget.


Tutorialwing Android Kotlin Radio Button Output Android Radio Button widget Using Kotlin

Tutorialwing Android Kotlin Radio Button Output

Getting Started

Android Radio Button can be defined as below –

An Android Radio Button is a two states button that can either be selected or unselected. There is no other option. Also, It can not be unselected, by clicking on it, once it is selected. That’s why we use this widget with radio group. When we use several radio buttons in a radio group, selecting one radio button make sure other radio buttons in the radio group are unselected.

Different Attributes of Android Radio Button widget

Attributes of Radio Button widget are inherited from TextView, Compound Button and View. Some of the popular
attributes of Radio Button inherited from TextView are –

Sr. XML Attributes Description
1. android:ems Defines width of view in ems.
2. android:gravity It decides how text inside this view will be aligned. For example, center, left, right etc.
3. android:height Specifies height of the view.
4. android:maxWidth Specifies maximum allowed width of the view.
5. android:minWidth Specifies minimum allowed width of the view.
6. android:width Specifies width of the view.

Attributes of Radio Button inherited from Compound Button are –

Sr. XML Attributes Description
1. android:button Drawable for button graphic
2. android:buttonTint Specifies Tint to button graphic
3. android:buttonTintMode Blending mode used to apply the button graphic tint.

Attributes of Radio Button inherited from View are –

Sr. XML Attributes Description
1. android:id Specifies unique id of the view.
2. android:padding Specifies padding of view.
3. android:onClick Decides what to do when view is clicked.
4. android:visibility Decides whether to show or hide the view.
5. android:tooltipText Text to be shown when cursor id hovered on this view.
6. android:background Sets background to view.
7. android:alpha Sets alpha in view.

Example of Android Radio Button Using Kotlin

At first, we will create a new android application. Then, we will use use radio button using kotlin file in this 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 RadioButton. 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 new project now, we will modify xml and kotlin file to use radio button widget in the application.

2. Modify values folder

	<string name="app_name">RadioButtonUsingKotlin</string>

Other values folder have not been changed. So, we are not going to mention it here.

3. Use Radio Button widget in xml file

Open res/layout/activity_main.xml file. Then, add code for radio button in it.

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






Here, we have tried to implement a scenario where you need to select gender(Male or Female) while filling job application. So, in activity_main.xml file, we have added two radio buttons inside a radio group. One radio button represents gender male and another represents female. 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.

4. Access Radio Button Widget in Kotlin file

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

package com.tutorialwing.radiobutton

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

In MainActivity.kt file, we have accessed radio group in which we have added two 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

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

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

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

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


When we run the application, we get the output as shown above.

That’s the end of tutorial on Android Radio Button using Kotlin with Example.