Radio Button Android Kotlin

Radio Button Android Kotlin: Android Studio Kotlin Example

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 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, lets 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
Radio Button Android Kotlin

 

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. 

 

 

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 checked state by default.

 

 

4- Finally, the complete activity_main.xml with a TextView, RadioGroup and five Radio Buttons will look like this:

 

activity_main.xml

 

 

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.

 

 

6- Then our complete MainActivity.kt file will be like this:

MainActivity.kt

 

 

So, this is it for our Radio Button Android Kotlin example. I hope now you have learnt 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 comments section below.

 

OUTPUT

 

Radio Button Android Kotlin
List Of Options. Radio Button Android Kotlin

 

When Button Checked. Radio Button Android Kotlin

 

 

 

More Android Posts

RecyclerView in Android With Kotlin

Android Kotlin SeekBar Example

Circular Determinate ProgressBar Kotlin

ListView in Android Using Kotlin

 

Leave a Comment

Your email address will not be published. Required fields are marked *