RadioButton Example in Kotlin

In this Kotlin programming tutorial I am going to share with you how to create a RadioButton in Kotlin programmatically. We will create RadioGroup of 5 radio buttons with one set as default and when the view is loaded the default RadioButton will be selected.

You will also learn how to create a RadioButton which is always checked. The Kotlin code example below will cover:

  • Create RadioGroup programmatically and add it to the LinearLayout,
  • Create RadioButton and add it to the RadioGroup,
  • Set a default value for the group,
  • Set an uncheckable value for the group.

XML Layout File

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/linearLayout"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.appsdeveloperblog.kotlinexample3.RadioButtonExample">
</LinearLayout>

RadioButton and RadioGroup Example in Kotlin

In the Kotlin code example below we have a RadioGroup and with 5 RadioButtons in it. One of RadioButtons is set as checked and appears selected when the view first loads. The second RadioGroup will contain only one RadioButton which is checked and cannot be unchecked.

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.LinearLayout
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView

class RadioButtonExample : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_radio_button_example)
        val linearLayout = findViewById(R.id.linearLayout) as LinearLayout
        val radioGroup = RadioGroup(this)
        val radioGroup2 = RadioGroup(this)
        val textView = TextView(this)

        textView.text = "option 1 is selected"
        radioGroup.orientation = RadioGroup.VERTICAL

        //This option will be the default value
        val radioButtonDefault = RadioButton(this)
        radioButtonDefault.text = "Default"
        radioGroup.addView(radioButtonDefault)
        radioGroup.check(radioButtonDefault.id)

        //These options will be the regular radio buttons
        val options = arrayOf("Option 2", "Option 3", "Option 4", "Option 5")
        for(option in options){
            val radioButton = RadioButton(this)
            radioButton.text = option
            radioGroup.addView(radioButton)
        }

        //This option can't be unchecked
        val radioButtonAlwaysChecked = RadioButton(this)
        radioButtonAlwaysChecked.text ="Always Checked"
        radioButtonAlwaysChecked.isChecked = true
        radioGroup2.addView(radioButtonAlwaysChecked)

        radioGroup.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { radioGroup, i ->
            textView.text = "option "+i+" is selected"
        })

        linearLayout.addView(radioGroup)
        linearLayout.addView(radioGroup2)
        linearLayout.addView(textView)
    }
}

If you run the above code example you should get the following view:

RadioButton Example in Kotlin

Don’t forget to check other Kotlin programming tutorials I have published in Kotlin category.

And if you are interested in learning Kotlin in more details, check out the below books and video lessons.

Happy learning Kotlin!

Building Mobile Apps with Kotlin for Android – Books


Building Mobile Apps with Kotlin for Android – Video Courses

Leave a Reply

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