Android RadioButton Example Kotlin

15-Sep-2024

Tutorial for how to implement RadioButton in Android Studio Project Kotlin

A RadioButton is a widget that belongs to the RadioGroup class and is designed to provide a set of mutually exclusive choices to the user. The user can only select her one option from a group of RadioButton elements.


Here is the tutorial for Java



Here is a simple example of using RadioButton in an Android app.

There Are Some Common Attributes for RadioButton

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
checked
true or flase (Default Checked)


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="10dp"
android:orientation="vertical">

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

<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" />

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

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


</RadioGroup>
</LinearLayout>




</RelativeLayout>



Step 2:

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



package com.microappvalley.newtestproject

import android.os.Bundle
import android.widget.Button
import android.widget.RadioButton
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
var radioButtonData=""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val buttonSubmit: Button =findViewById(R.id.buttonSubmit)
val radio_mango: RadioButton =findViewById(R.id.radio_mango)
val radio_banana: RadioButton =findViewById(R.id.radio_banana)
val textResult: TextView =findViewById(R.id.textResult)

radio_mango.setOnCheckedChangeListener { compoundButton, isChecked ->
if (isChecked){
radioButtonData="Mango";
Toast.makeText(this@MainActivity, "Mango Button "+isChecked, Toast.LENGTH_SHORT).show();
}
}

radio_banana.setOnCheckedChangeListener { compoundButton, isChecked ->
if (isChecked){
radioButtonData="Banana";
Toast.makeText(this@MainActivity, "Banana Button "+isChecked, Toast.LENGTH_SHORT).show();
}
}

buttonSubmit.setOnClickListener {

textResult.text=radioButtonData
Toast.makeText(this@MainActivity, radioButtonData, 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