In Android, we use Radio buttons when we have to select one option from the list of available options. A RadioButton has two states. A RadioButton can be checked or unchecked by the user. By default, the RadioButton has OFF (Unchecked State). So, if a user clicks a RadioButton it comes in a checked state then it cannot be unchecked by clicking it again. A checked RadioButton becomes unchecked when you click any other RadioButton in the same Radio Group. So, let’s understand Radio Button in Android with Kotlin language.
The advantage of using Radio Buttons is that a user can create a list of available options. All these options are available to the user but the user can select (check) only one option from the list.
We use Radio Buttons together using RadioGroup widget. RadioGroup is responsible of grouping Radio Buttons and it provides the feature to select one Radio Button at a time. If a RadioButton is in checked state and any other RadioButton is clicked within the same group then the previously checked RadioButton becomes unchecked.

Radio Button Android Kotlin:
1- Create a new Android Studio Project. Name your project, so in my case, the name is “RadioButtonKotlin”. Select any API level according to your choice and choose your default language as KOTLIN.
2- In your activity_main.xml change the default Constraint Layout to Relative Layout. Here we add a TextView to display some text and a RadioGroup.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="130dp" android:text="Select Your Choice" android:textColor="#000000" android:textSize="30sp" /> <RadioGroup android:id="@+id/radiogroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="180dp" android:padding="16dp"> |
3- Inside our Radio Group we will take five Radio Buttons. We add text for our Radio Buttons and assign ids to them. We have put one Radio Button in a checked state by default.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<RadioButton android:id="@+id/radio_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Java" android:textSize="25sp" android:textColor="#000000"/> <RadioButton android:id="@+id/radio_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Kotlin" android:textSize="25sp" android:textColor="#000000"/> <RadioButton android:id="@+id/radio_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Python" android:textSize="25sp" android:textColor="#000000"/> <RadioButton android:id="@+id/radio_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Swift" android:textSize="25sp" android:textColor="#000000" android:checked="true" /> <RadioButton android:id="@+id/radio_5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Javascript" android:textSize="25sp" android:textColor="#000000"/> |
4- Finally, the complete activity_main.xml with a TextView, RadioGroup and five Radio Buttons will look like this:
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="130dp" android:text="Select Your Choice" android:textColor="#000000" android:textSize="30sp" /> <RadioGroup android:id="@+id/radiogroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="180dp" android:padding="16dp"> <RadioButton android:id="@+id/radio_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Java" android:textSize="25sp" android:textColor="#000000"/> <RadioButton android:id="@+id/radio_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Kotlin" android:textSize="25sp" android:textColor="#000000"/> <RadioButton android:id="@+id/radio_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Python" android:textSize="25sp" android:textColor="#000000"/> <RadioButton android:id="@+id/radio_4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Swift" android:textSize="25sp" android:textColor="#000000" android:checked="true" /> <RadioButton android:id="@+id/radio_5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Javascript" android:textSize="25sp" android:textColor="#000000"/> </RadioGroup> </RelativeLayout> |
5- So, now in our MainActivity.kt file we simply add onClickListener on our Radio Buttons. With the help of an onClickListener we will display a Toast message. We set onClickListener in our onCreate function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
radio_1.setOnClickListener{ Toast.makeText(this,"You Selected: Java",Toast.LENGTH_SHORT).show() } radio_2.setOnClickListener{ Toast.makeText(this,"You Selected: Kotlin",Toast.LENGTH_SHORT).show() } radio_3.setOnClickListener{ Toast.makeText(this,"You Selected: Python",Toast.LENGTH_SHORT).show() } radio_4.setOnClickListener{ Toast.makeText(this,"You Selected: Swift",Toast.LENGTH_SHORT).show() } radio_5.setOnClickListener{ Toast.makeText(this,"You Selected: Javascript",Toast.LENGTH_SHORT).show() } |
6- Then our complete MainActivity.kt file will be like this:
MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.example.radiobuttonkotlin import androidx.appcompat.app.AppCompatActivity import android.os.Bundle 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) radio_1.setOnClickListener{ Toast.makeText(this,"You Selected: Java",Toast.LENGTH_SHORT).show() } radio_2.setOnClickListener{ Toast.makeText(this,"You Selected: Kotlin",Toast.LENGTH_SHORT).show() } radio_3.setOnClickListener{ Toast.makeText(this,"You Selected: Python",Toast.LENGTH_SHORT).show() } radio_4.setOnClickListener{ Toast.makeText(this,"You Selected: Swift",Toast.LENGTH_SHORT).show() } radio_5.setOnClickListener{ Toast.makeText(this,"You Selected: Javascript",Toast.LENGTH_SHORT).show() } } } |
So, this is it for our Radio Button Android Kotlin example. I hope now you have learned how to use Radio Button in Android Studio but still, if you have any queries regarding Android Radio Button then you can definitely ask in the comments section below.
OUTPUT


More Android Posts
RecyclerView in Android With Kotlin
Android Kotlin SeekBar Example
Circular Determinate ProgressBar Kotlin
ListView in Android Using Kotlin