Skip to content

Android RadioButton & RadioGroup with Example in Kotlin

Android RadioButton is given a choice to users to select an option. It can be shown side by side or in other ways but it takes space on the screen. Another option is use spinner, if there is so many (like 4-30 or more) option and space on the screen are less.

Radio Buttons are mutually exclusive ( One RadioButton is not influenced or caused by another RadioButtons), you have to group them together using a RadioGroup. By grouping – The System will ensure that only one Radio button selects in a group.

Android RadioButton &  RadioGroup with Example in Kotlin

You can add RadioButton in resource file or dynamically create in class file.

RadioButton has 2 states – checked or unchecked. By default Radio Buttons are unchecked, User can check it by simply touch (Click) on it. Once a RadiaButton is checked by the user it can’t be unchecked by simply click on it. It will uncheck RadioButton only within the same RadioGroup.

Using RadioButton 

Add the RadioButton in resource layout file.

<RadioButton
        android:id="@+id/radioButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

Then set onClicklistener on RadioButton , check below code. (Note : code is in kotlin)

radioButton.setOnClickListener(View.OnClickListener {
            message("Selected")
        })

Radio button selected by default.

To make a radio button is selected by default, put android:checked="true" within the RadioButton element.

Let’s Build a Complete Example of Android Checkbox :

Example: what if you have multiple RadioButton? Let check this example with simple one and multiple RadioButton in a app.

Step 1. Create new project “Build Your First Android App in Kotlin
Step 2. Add below code in “activity_main.xml” resource file

Here  7 checkbox is used, where 6 checkbox is perform a event , added the android:onClick attribute. And 1 will be use OnClickListener.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_nogroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="No Readio Group" />

    <RadioButton
        android:id="@+id/radioButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

    <TextView
        android:id="@+id/tv_gender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:text="Gender"
        android:textSize="16sp" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rb_male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Male"
            android:onClick="onRadioButtonClicked"/>

        <RadioButton
            android:id="@+id/rb_female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Female"
            android:onClick="onRadioButtonClicked"/>
    </RadioGroup>

    <TextView
        android:id="@+id/tv_sport"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:text="Select one sport"
        android:textSize="16sp"
        android:onClick="onRadioButtonClicked"/>

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/rb_football"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Football"
            android:onClick="onRadioButtonClicked"/>

        <RadioButton
            android:id="@+id/rb_cricket"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cricket"
            android:onClick="onRadioButtonClicked"/>

        <RadioButton
            android:id="@+id/rb_hockey"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hockey"
            android:onClick="onRadioButtonClicked"/>

        <RadioButton
            android:id="@+id/rb_badminton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Badminton"
            android:onClick="onRadioButtonClicked"/>
    </RadioGroup>
</LinearLayout>
Step 3. Open the “MainActivity.kt” and add following code
package `in`.eyehunt.androidradiobutton

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.RadioButton
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

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

        //Single radio button no group
        //on click on button
        radioButton.setOnClickListener(View.OnClickListener {
            message("Selected")
        })
    }

    // multiple Radio click method
    fun onRadioButtonClicked(view: View) {
        var checked = view as RadioButton
        if (rb_male == checked) {
            message(rb_male.text.toString() + if (rb_male.isChecked) " Checked " else " UnChecked ")
        }
        if (rb_female == checked) {
            message(rb_female.text.toString() + if (rb_female.isChecked) " Checked " else " UnChecked ")
        }

        if (rb_football == checked) {
            message(rb_football.text.toString() + if (rb_football.isChecked) " Checked " else " UnChecked ")
        }
        if (rb_cricket == checked) {
            message(rb_cricket.text.toString() + if (rb_cricket.isChecked) " Checked " else " UnChecked ")
        }
        if (rb_hockey == checked) {
            message(rb_hockey.text.toString() + if (rb_hockey.isChecked) " Checked " else " UnChecked ")
        }
        if (rb_badminton == checked) {
            message(rb_badminton.text.toString() + if (rb_badminton.isChecked) " Checked " else " UnChecked ")
        }
    }

    fun message(str: String) {
        Toast.makeText(this, str, Toast.LENGTH_LONG).show()
    }
}
Step 4. Now Run the application, in emulator or On you android device

Output screenshot Android RadioButton example :

Android RadioButton & RadioGroup with Example in Kotlin output

Download source code Android RadioButton in kotlin

https://github.com/EyeHunts/AndroidRadioButton

Do comment if you have any doubt and suggestion on this tutorial.

Note: This example (Project) is developed in Android Studio 3.1.3. Tested on Android 9 ( Android-P), compile SDK version API 27: Android 8.0 (Oreo)

MinSdkVersion=”15″

TargetSdkVersion=”27″

Coding in Kotlin

2 thoughts on “Android RadioButton & RadioGroup with Example in Kotlin”

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading