Android RadioButton & RadioGroup with Example in Kotlin

Android RadioButton is give a choice to user select an option. It can be show side by side or others ways but its take a space in screen. Other option is use spinner, if there is so many (like 4-30 or more) option and space on screen is 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 ensures that only one Radio button select in a group.

Android RadioButton &  RadioGroup with Example in Kotlin

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

RadioButton have 2 state – 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 user it can’t be unchecked by simply click on it. It will unchecked RadioButton only within same RadioGroup.

Using RadioButton 

Add the RadioButton in resource layout file.

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

Radio button selected by default.

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

Lets 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.

Step 3. Open the “MainActivity.kt” and add following code
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

  1. What would I need to do if instead of displaying a message, I had to get information that is on a particular radioButton and use it on something else?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.