Q12.1 Write a program to show the following output.
First two radio buttons are without using radiogroup and
next two radiobuttons are using radiogroup. Note the changes between these two. Also Toast which radiobutton
has been selected.
activity_main.xml MainActivity.java
<?xml version="1.0" encoding="utf-8"?> package com.example.exp12_1;
<RelativeLayout import android.os.Bundle;
xmlns:android="http://schemas.android.com/apk/res/android" import android.view.View;
xmlns:app="http://schemas.android.com/apk/res-auto" import android.widget.Button;
xmlns:tools="http://schemas.android.com/tools" import android.widget.RadioButton;
android:id="@+id/main" import android.widget.Toast;
android:layout_width="match_parent" import androidx.activity.EdgeToEdge;
android:layout_height="match_parent" import
android:background="@color/white" androidx.appcompat.app.AppCompatActivity;
android:padding="10dp" public class MainActivity extends
tools:context=".MainActivity"> AppCompatActivity {
<TextView RadioButton rb1, rb2, rb3, rb4;
android:layout_width="wrap_content" Button btn;
android:layout_height="wrap_content" String str;
android:text="Single radio button" @Override
android:layout_marginTop="20dp" protected void onCreate(Bundle
android:layout_centerHorizontal="true" savedInstanceState) {
android:textSize="20sp"/> super.onCreate(savedInstanceState);
<RadioButton EdgeToEdge.enable(this);
android:id="@+id/r1" setContentView(R.layout.activity_main);
android:layout_width="wrap_content" rb1=findViewById(R.id.r1);
android:layout_height="wrap_content" rb2=findViewById(R.id.r2);
android:text="Radio button 1" rb3=findViewById(R.id.r3);
android:layout_marginTop="50dp" rb4=findViewById(R.id.r4);
android:textSize="20sp"/> btn=findViewById(R.id.bt);
<RadioButton btn.setOnClickListener(new
android:id="@+id/r2" View.OnClickListener() {
android:layout_width="wrap_content" @Override
android:layout_height="wrap_content" public void onClick(View v) {
android:text="Radio button 2" Show(v);
android:layout_marginTop="10dp" }
android:layout_below="@id/r1" });
android:textSize="20sp"/> }
<View public void Show(View v){
android:id="@+id/v1" str="";
android:layout_width="match_parent" if (rb1.isChecked()){
android:layout_height="2dp" str+="Radio button1,";
android:layout_below="@id/r2" }
android:background="@android:color/black" if (rb2.isChecked()){
android:layout_marginTop="10dp"/> str+="Radio button2,";
<TextView }
android:id="@+id/t2" if (rb3.isChecked()){
android:layout_width="wrap_content" str+="Male,";
android:layout_height="wrap_content" }
android:text="Radio Button inside RadioGroup" if (rb4.isChecked()){
android:textSize="20sp" str+="Female";
android:layout_below="@id/v1" }
android:layout_marginTop="10dp" Toast.makeText(this,"Selected radio Button
android:layout_centerHorizontal="true"/> is"+str, Toast.LENGTH_LONG).show();
<RadioGroup }
android:id="@+id/radioGroup" }
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/t2"
android:layout_marginTop="10dp"
android:orientation="vertical">
<RadioButton
android:id="@+id/r3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:textSize="20sp"/>
<RadioButton
android:id="@+id/r4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:textSize="20sp"/>
</RadioGroup>
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Show"
android:text="Show details"
android:textSize="15sp"
android:layout_centerHorizontal="true"
android:layout_below="@id/radioGroup"
android:layout_marginTop="20dp"/>
</RelativeLayout>
Exp12 Output