Open In App

Dynamic RadioButton in Kotlin

Last Updated : 25 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

An Android RadioButton is a bi-state button that can be either checked or unchecked. It functions similarly to a CheckBox, but with one key difference: once selected, a RadioButton cannot be unchecked by tapping it again. RadioButtons are typically used within a RadioGroup to allow users to select only one option from multiple choices. By default, a RadioButton is in the OFF (unchecked) state, but you can change its default state using the android:checked attribute in XML. This makes RadioButtons ideal for scenarios where a single selection is required, such as choosing a payment method, selecting a gender, or picking a preferred option in a form.

You can create an RadioButton in Android using two approaches:

  1. XML Layout: Define it in the activity's XML file.
  2. Programmatically: Create and configure it in the Kotlin file.

In this article, we will create an RadioButton programmatically using Kotlin.

Step by Step Implementation

Step 1: Create a new project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.

Step 2: Working with activity_main.xml file

First of all define the RadioGroup in the Linearlayout and access into the Kotlin file.

activity_main.xml:

XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    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"
    android:id="@+id/main"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="32dp">
    </RadioGroup>
</LinearLayout>

Layout Design:

Layout


Step 3: Working with MainActivity.kt file

Here, we define three radio buttons for the color and set their attributes.

val radioButton1 = RadioButton(this)
radioButton1.layoutParams= layoutParams
radioButton1.text = "Black"
radioButton1.id = 1

then, use them into RadioGroup using code:

val radioGroup: RadioGroup = findViewById(R.id.radioGroup)
radioGroup.addView(radioButton1)
radioGroup.addView(radioButton2)
radioGroup.addView(radioButton3)

MainActivity.kt:

Kotlin
package org.geeksforgeeks.demo

import android.os.Bundle
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val layoutParams = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )

        // Create RadioButton programmatically
        val radioButton1 = RadioButton(this)
        radioButton1.layoutParams= layoutParams
        radioButton1.text = "Black"
        radioButton1.id = 1

        val radioButton2 = RadioButton(this)
        radioButton2.layoutParams = layoutParams
        radioButton2.text = "White"
        radioButton2.id = 2

        val radioButton3 = RadioButton(this)
        radioButton3.layoutParams = layoutParams
        radioButton3.text = "Blue"
        radioButton3.id = 3

        val radioGroup: RadioGroup = findViewById(R.id.radioGroup)
        radioGroup.addView(radioButton1)
        radioGroup.addView(radioButton2)
        radioGroup.addView(radioButton3)

        radioGroup.setOnCheckedChangeListener { _, checkedId ->
            var string = "Selected: "
            string += " " +
                    when (checkedId) {
                        1 -> "Black"
                        2 -> "White"
                        else -> "Blue"
                    }
            Toast.makeText(this, string, Toast.LENGTH_SHORT).show()
        }
    }
}

Output:


Next Article

Similar Reads