VOOZH about

URL: https://www.geeksforgeeks.org/kotlin/radiobutton-in-kotlin/

⇱ RadioButton in Kotlin - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

RadioButton in Kotlin

Last Updated : 23 Jan, 2025

Android Radio Button is bi-state button which can either be checked or unchecked. Also, it's working is same as Checkbox except that radio button can not allow to be unchecked once it was selected. Generally, we use RadioButton controls to allow users to select one option from multiple options. By default, the RadioButton in OFF (Unchecked) state but we can change the default state of RadioButton by using android:checked attribute.

Following steps to create new project:

  • Click on File, then New => New Project.
  • Then, check Include Kotlin Support and click next button.
  • Select minimum SDK, whatever you need.
  • Select Empty activity and then click finish.

Different Attributes of RadioButton Widget

XML AttributesDescription
android:idUsed to uniquely identify the control
android:gravityUsed to specify how to align the text like left, right, center, top, etc.
android:checkedUsed to specify the current state of radio button
android:onClickIt’s a name of the method to invoke when the radio button clicked.
android:textSizeUsed to set size of the text.
android:textColorUsed to set color of the text.
android:textStyleUsed to set style of the text. For example, bold, italic, bolditalic etc.
android:maxWidthUsed to make the view be at most this many pixels wide.
android:minWidthUsed to make the view be at least this many pixels wide.
android:backgroundUsed to set the background of the radio button control.
android:visibilityUsed to control the visibility.

Step by Step to Demonstrate Radio Button

We will follow some steps to demonstration of application using Radio Button, follow the steps mentioned below:

Step 1: Add RadioButtons in activity_main.xml file

In android, we use radio buttons inside RadioGroup to combine set of radio buttons into single group and it will make sure that user can select only button from the group of buttons.

activity_main.xml:


Here, we are trying to implement a scenario where you need to select your favorite color. So, in activity_main.xml file, we have added 4 radio buttons inside a radio group. Each button represent a color. Now, only one radio button can be selected at a time.

Layout:

πŸ‘ Layout_Button


Now, we will access this widget in kotlin file and show a proper message whenever a radio button is selected.

Step 2: Open MainActivity.kt file and add below code into it.

MainActivity.kt:


In MainActivity.kt file, we have accessed radio group in which I have added four radio buttons. Then, we have set a listener to display toast message whenever radio button selection changes. Since AndroidManifest.xml file is very important in any android application, we are also going to mention it here.

Run as Emulator: (Two Cases are shown selecting the element and clicking the button)


Comment
Article Tags:

Explore