Activity_main.
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20sp">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Single Radio Buttons"
android:textAlignment="center"
android:textSize="20sp"
/>
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 1"
android:textSize="20sp"
android:textStyle="bold"
/>
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 2"
android:textSize="20dp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button inside RadioGroup"
android:textAlignment="center"
android:textSize="20sp"
/>
<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:textSize="20sp"
android:textStyle="bold"
/>
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:textSize="20sp"
android:textStyle="bold"
/>
</RadioGroup>
<Button
android:id="@+id/bshow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Selected"
android:textAlignment="center"
android:layout_marginTop="10dp"
/>
</LinearLayout>
mainActivity.java
package com.example.pract12;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
private RadioButton rb1,rb2,male,female;
private RadioGroup rg;
private Button bshow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rb1=findViewById(R.id.rb1);
rb2=findViewById(R.id.rb2);
male=findViewById(R.id.male);
female=findViewById(R.id.female);
rg=findViewById(R.id.rg);
bshow=findViewById(R.id.bshow);
//noinspection AnonymousInnerClass
bshow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String selectedText = "";
if (rb1.isChecked()) {
selectedText = "Selected: Radio Button 1";
} else if (rb2.isChecked()) {
selectedText = "Selected: Radio Button 2";
}
int selectedId = rg.getCheckedRadioButtonId();
if (selectedId != -1) {
RadioButton selectedRadioButton =
findViewById(selectedId);
selectedText += "\nSelected: " +
selectedRadioButton.getText().toString();
} // Show toast message//
if (!selectedText.isEmpty()) {
Toast.makeText(MainActivity.this, selectedText,
Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this, "No selection made",
Toast.LENGTH_LONG).show();
}
}
});
}
}
Output: