Android RadioGroup Example Kotlin

15-Sep-2024

Tutorial on how to implement RadioGroup in Android Studio Project Kotlin

On Android, a RadioGroup is a widget that groups multiple radio buttons. This allows the user to choose one option from a set of mutually exclusive options.


Here is the tutorial for Java



Here is a simple example of using a RadioGroup in a RadioButton element:


There Are Some Common Attributes for RadioGroup

id
This ID serves as the control's unique identity.
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
layout_widthmatch_parent -> Text area contains fullscreen
wrap_content-> Text area contains only text size
textSize
Change The Text Size
textColor
Change Text Color


Step 1 :

File open res/layout/activity_main.xml file −



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_margin="20dp"
android:orientation="vertical">

<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_mango"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mango"/>
<RadioButton android:id="@+id/radio_banana"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Banana"/>
<RadioButton android:id="@+id/radio_orange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Orange"/>
</RadioGroup>


<Button
android:id="@+id/buttonSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />

<TextView
android:id="@+id/textResult"
android:layout_width="match_parent"
android:gravity="center"
android:textSize="20dp"
android:layout_marginTop="20dp"
android:layout_height="wrap_content"
android:text="Show Result" />
</LinearLayout>



</RelativeLayout>



Step 2 :

File open com.microappvalley.newtestproject/MainActivity.java file −


package com.microappvalley.newtestproject

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.RadioButton
import android.widget.RadioGroup

import android.widget.TextView
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 buttonSubmit: Button =findViewById(R.id.buttonSubmit)
val radioGroup: RadioGroup =findViewById(R.id.radioGroup)
val textResult: TextView =findViewById(R.id.textResult)

buttonSubmit.setOnClickListener {
var id=0;
id=radioGroup.checkedRadioButtonId

if (id==View.NO_ID){
Toast.makeText(this@MainActivity,"No Chackd",Toast.LENGTH_SHORT).show()

}else{
val selectedRadioButton: RadioButton=findViewById(id)
textResult.text=selectedRadioButton.text.toString()
Toast.makeText(this@MainActivity,selectedRadioButton.text,Toast.LENGTH_SHORT).show()

}
}
}
}


Open Device Manager, run the emulator, and then run the application. Next, check the working output and check the output you declared in your code.


Output:


Comments