Android Koltin RadioButton

Android RadioButton allows user select one option from a set , RadioButton Generally used along with RadioGroup .

RadioGroup group contains multiple RadioButton , when user click one radio button that will it marked as checked and rest others are unchecked

Lets see a sample example

1. XML Layout


  1. Create XML Layout activity_main.xml , this will be set as content view for launcher Activity (MainActivity.kt)
  2. Add RadioGroup Tag and Inside radioGroup add RadioButton Tag .

file : activity_main.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:orientation="vertical"
        tools:context=".MainActivity"
        android:padding="30dp"
        android:id="@+id/root">

    <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/gender">

        <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/radio_option_male"
                android:id="@+id/male"
                android:textSize="16sp"/>

        <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/radio_option_female"
                android:id="@+id/female"
                android:textSize="16sp"/>

    </RadioGroup>

</LinearLayout>


OnCheckedChangeListener


Set setOnCheckedChangeListener for radioGroup , show toast message for select radioButton.

  gender.setOnCheckedChangeListener({ group, checkedId ->
            val radioButton = findViewById<View>(checkedId) as RadioButton
            Toast.makeText(this@MainActivity, radioButton.text.toString() + " is selected", Toast.LENGTH_SHORT).show()
        })

3. Activity


  1. Create a koltin class MainActivity.kt and extend it to AppCompatActivity class .
  2. Override onCreate function and set the content of this MainActivity with above defined xml layout (activity_main.xml).
  3. Set setOnCheckedChangeListener for radioGroup , show toast message for select radioButton.

file : MainActivity.kt
package com.tutorialsbuzz.androidradiobutton

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.RadioButton
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

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

        gender.setOnCheckedChangeListener({ group, checkedId ->
            val radioButton = findViewById<View>(checkedId) as RadioButton
            Toast.makeText(this@MainActivity, radioButton.text.toString() + " is selected", Toast.LENGTH_SHORT).show()
        })

    }
}


Programmatically Adding RadioGroup RadioButton To Layout



//RadioGroup
val radioGroup = RadioGroup(this)
radioGroup.layoutParams =
 ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)

//RadioButton
val radioButton1 = RadioButton(this)
radioButton1.layoutParams =
 ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
radioButton1.setText("Option 1")

//RadioButton
val radioButton2 = RadioButton(this)
radioButton2.layoutParams =
 ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
radioButton2.setText("Option 1")

//Adding RadioButtons To RadioGroup
radioGroup.addView(radioButton1)
radioGroup.addView(radioButton2)

//Adding RadioGroup To Parent layout
root.addView(radioGroup)





No comments:

Post a Comment