Open In App
Related Articles

Dynamic RadioButton in Kotlin

Like Article
Save Article
Report issue
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. In order to create a radio button, first we need to create a project. We are naming it a DynamicRadioButton
  • 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.

Modify the activity_main.xml file

First of all define the RadioGroup in the Linearlayout and access into the Kotlin file.
<?xml version="1.0" encoding="utf-8"?>

The above code will still produce some errors, to resolve these add the following code snippet to app/res/values/strings.xml
    <string name="app_name">DynamicRadioButton</string>
    <string name="black">Black</string>
    <string name="white">White</string>
    <string name="blue">Blue</string>
    <string name="you_selected">You selected:</string>


MainActivity.kt file

Here, we define three radio buttons for the color and set their attributes.
 val radioButton1 = RadioButton(this)
then, use them into RadioGroup using code:
package com.geeksforgeeks.myfirstkotlinapp
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?)
        val linearLayout = findViewById<LinearLayout>(
        // Create RadioButton programmatically
        val radioButton1 = RadioButton(this)
        radioButton1.layoutParams= LinearLayout.LayoutParams(
            radioButton1.setText("Black") = 1
        val radioButton2 = RadioButton(this)
        radioButton2.layoutParams = LinearLayout.LayoutParams(
            radioButton2.setText("White") = 2
        val radioButton3 = RadioButton(this)
        radioButton3.layoutParams = LinearLayout.LayoutParams(
            radioButton3.setText("Blue") = 3
        val radioGroup = findViewById<RadioGroup>(
        if (radioGroup != null)
            radioGroup.setOnCheckedChangeListener { group, checkedId ->
                var string = getString(R.string.you_selected)
                string += " " + getString(
                    if (checkedId == 1)
                    else if(checkedId == 2 ) R.string.white
                    else )
            Toast.makeText(applicationContext, string, Toast.LENGTH_SHORT).show()


AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
    <activity android:name=".MainActivity">
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />


Run as Emulator for output:


Last Updated : 19 Feb, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads