Native Mobile App Development With Java

Android RadioButton Example

15-Sep-2024

Tutorial for how to implement RadioButton in Android Studio Project Java

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 Kotlin



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.java file −

Java


package com.microappvalley.newtestproject;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
Button buttonSubmit;
RadioButton radio_mango;
RadioButton radio_banana;
RadioButton radio_orange;
TextView textResult;
ImageButton imageButton;
String radioButtonData="";
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonSubmit=(Button) findViewById(R.id.buttonSubmit);
radio_mango=(RadioButton) findViewById(R.id.radio_mango);
radio_banana=(RadioButton) findViewById(R.id.radio_banana);
textResult=(TextView) findViewById(R.id.textResult);


radio_mango.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked){
radioButtonData="Mango";
Toast.makeText(MainActivity.this, "Mango Button "+isChecked, Toast.LENGTH_SHORT).show();
}
}
});

radio_banana.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked){
radioButtonData="Banana";
Toast.makeText(MainActivity.this, "Banana Button "+isChecked, Toast.LENGTH_SHORT).show();
}

}
});


buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {


textResult.setText(radioButtonData);
Toast.makeText(MainActivity.this, 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