Open In App
Related Articles

Dynamic RadioGroup in Kotlin

Like Article
Save Article
Report issue

In Android RadioGroup is used to set radio buttons. If one radio button is selected within the RadioGroup, all other ones are automatically deselected.

In this article, we will be discussing how to programmatically create a RadioGroup in Kotlin.

Let’s start by first creating a project in Android Studio. To do so, follow these instructions:

  • Click on File, then New, and then New Project, and give name whatever you like
  • Then, select Kotlin language Support and click next button.
  • Select minimum SDK, whatever you need
  • Select Empty activity and then click finish.

After that, we need to design our layout. For that we need to work with the XML file. Go to app > res > layout and paste the following code: 

Modify activity_main.xml file


<?xml version="1.0" encoding="utf-8"?>



The next step is to code our RadioGroup. Open app/src/main/java/yourPackageName/MainActivity.kt and paste the following code:


package com.geeksforgeeks.myfirstKotlinapp
import android.os.Bundle
import android.view.ViewGroup
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.RelativeLayout
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        val layout = findViewById<RelativeLayout>(
        // Create RadioButton Dynamically
        val geek1 = RadioButton(this)
        //setting height and width
        geek1.layoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        geek1.setText( //setting text of first radio button = 0
        val geek2 = RadioButton(this)
        geek2.layoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        geek2.setText(R.string.python) ////setting text of second radio button = 1
        // Create RadioGroup Dynamically
        val radioGroup = RadioGroup(this)
        val params = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        params.setMargins(40, 0, 0, 0)
        radioGroup.layoutParams = params
        //adding button to the radio group container
        radioGroup.setOnCheckedChangeListener { group, checkedId ->
            var text = getString(R.string.Chose)
            text += " " + getString(if (checkedId == 0) {
            } else {
            Toast.makeText(applicationContext, text, Toast.LENGTH_SHORT).show()


The above code will still produce some errors because we have used some strings which have been declared in another file, to resolve these add the following code snippet to app/res/values/strings.xml 


    <string name="app_name">My Application</string>
    <string name="java">Java</string>
    <string name="python">Python</string>
    <string name="Choose">You selected:</string>




<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.dynamicradiogroup"
xmlns:android="<a class="vglnk" href="" rel="nofollow">
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>


Run as Emulator:

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