Android RadioButton, RadioGroup Using Kotlin With Example

In this tutorial, we’ll be discussing and implementing RadioButton and RadioGroups in our Android Application using Kotlin.

Android RadioButton

A RadioButton is a widget which can be set to checked or an unchecked state. Once a RadioButton is checked you cannot uncheck it unless it’s present inside a RadioGroup.
A RadioGroup is a container that holds RadioButtons. At a time inside a RadioGroup, only one RadioButton can be set as checked.
A RadioButton is defined in the xml in the following manner:

android:text is used to set the text of the RadioButton.
You can set the text gravity using the android:gravity attribute
android:onClick is used to set the function in the Kotlin activity to be triggered when the RadioButton is clicked.
android:buttonTint is used to set the color of the circular button. By default, it is set to the colorAccent specified in the styles.xml.

To define a RadioButton programmatically we use:

A RadioGroup is defined in the following way in the XML.

Setting the orientation on the RadioGroup would lay the RadioButtons in that order(horizontally/vertically).

To define a RadioGroup programmatically, we do:

This adds a single RadioButton inside the RadioGroup.

A RadioGroup can set Layout Weights similar to a LinearLayout.
We use the attribute android:weightSum on the RadioGroup and android:layout_weight on the RadioButton(s).

To clear ALL states from a RadioGroup we need to invoke the following in our Kotlin Activity class.

RadioGroup Listener
In our activity, we can use the RadioGroup.OnCheckChangedListener interface callback to listen for changes in the states of the RadioButtons held inside the RadioGroup.

radioGroup argument is the current radiogroup and i is the id of the RadioButton present in that RadioGroup.

In the following section, we’ll be creating a Single View Application that hosts RadioGroups from the XML as well as programmatically. We’ll display a Toast whenever any of the RadioButtons is checked.

Project Structure


Layout Code

The code for the activity_main.xml layout is given below:

Activity Code

The code for the MainActivity.kt class is given below:

In the above code, we’ve created a Second RadioGroup which holds the RadioButtons horizontally.
We’ve implemented RadioGroup.OnCheckedChangeListener interface on our Activity.
androidlyRadioButton is triggered when the single RadioButton defined in the layout gets checked.
We need to typecast it from View to RadioButton.
fun onCheckedChanged(group: RadioGroup?, checkId: Int) is what gets triggered everytime a RadioButton from any of the RadioGroups gets checked or unchecked.
checkedRadioButtonId property is used to get the ID of the RadioButton that was selected.
We use the isChecked property on the RadioButton to display a Toast only when a RadioButton gets checked.

The output of the above application in action is given below:

This brings an end to this tutorial. You can download the AndroidlyRadioButtons Project from the link below.

By admin

Leave a Reply

%d bloggers like this: