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 Kotlin
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_width | match_parent -> Text area contains fullscreen wrap_content-> Text area contains only text size |
layout_width | match_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 androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button buttonSubmit;
RadioGroup radioGroup;
TextView textResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonSubmit=(Button) findViewById(R.id.buttonSubmit);
radioGroup=(RadioGroup) findViewById(R.id.radioGroup);
textResult=(TextView) findViewById(R.id.textResult);
buttonSubmit.setOnClickListener(new View.OnClickListener() {
@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View view) {
int id=radioGroup.getCheckedRadioButtonId();
if(id==view.NO_ID){
Toast.makeText(MainActivity.this, "No Chackd", Toast.LENGTH_SHORT).show();
}else {
RadioButton selectedRadioButton = findViewById(id);
textResult.setText(selectedRadioButton.getText().toString());
Toast.makeText(MainActivity.this, selectedRadioButton.getText().toString(), 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.