0% found this document useful (0 votes)
15 views3 pages

Mad Prac12

The document provides code for an Android application that demonstrates the use of radio buttons, both with and without a RadioGroup. It includes XML layout for the user interface and Java code for handling user interactions, specifically displaying a Toast message indicating which radio button was selected. The program features two standalone radio buttons and two within a RadioGroup, highlighting the differences in their functionality.

Uploaded by

Vedant Nagul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Mad Prac12

The document provides code for an Android application that demonstrates the use of radio buttons, both with and without a RadioGroup. It includes XML layout for the user interface and Java code for handling user interactions, specifically displaying a Toast message indicating which radio button was selected. The program features two standalone radio buttons and two within a RadioGroup, highlighting the differences in their functionality.

Uploaded by

Vedant Nagul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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

You might also like