Android Radio Group Using Kotlin With Example




Target

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.

Output

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"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:gravity="center">

	<RadioGroup
		android:id="@+id/radioGroup"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:paddingLeft="20dp">

		<RadioButton
			android:id="@+id/radioMale"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:padding="10dp"
			android:text="Male"/>

		<RadioButton
			android:id="@+id/radioFemale"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:padding="10dp"
			android:text="Female"/>

	</RadioGroup>

</LinearLayout>

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.support.v7.app.AppCompatActivity
import android.widget.RadioGroup
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val radioGroup = findViewById<RadioGroup>(R.id.radioGroup)
        radioGroup?.setOnCheckedChangeListener { group, checkedId ->
            var text = "You selected: "
            text += if (R.id.radioMale == 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"
		  xmlns:android="http://schemas.android.com/apk/res/android">

	<application
		android:allowBackup="true"
		android:icon="@mipmap/ic_launcher"
		android:label="@string/app_name"
		android:roundIcon="@mipmap/ic_launcher_round"
		android:supportsRtl="true"
		android:theme="@style/AppTheme">
		<activity android:name=".MainActivity">
			<intent-filter>
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>
			</intent-filter>
		</activity>
	</application>

</manifest>

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

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