0% found this document useful (0 votes)
11 views5 pages

12th Practical

The document contains an XML layout for an Android application featuring a RelativeLayout with TextViews, RadioButtons, and a Button. The Java code defines a MainActivity that handles the button click event to display the selected RadioButton's text using a Toast message. The layout includes a group of RadioButtons for gender selection and demonstrates basic UI interactions in Android development.

Uploaded by

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

12th Practical

The document contains an XML layout for an Android application featuring a RelativeLayout with TextViews, RadioButtons, and a Button. The Java code defines a MainActivity that handles the button click event to display the selected RadioButton's text using a Toast message. The layout includes a group of RadioButtons for gender selection and demonstrates basic UI interactions in Android development.

Uploaded by

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

Practical No.

12

//xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Single RadioButton"
android:textStyle="bold"
android:textSize="30dp"
android:id="@+id/t1"
android:layout_centerHorizontal="true" />
<RadioButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/r1"
android:text="Radio Button 1"
android:textSize="20dp"
android:textStyle="bold"
android:layout_below="@id/t1">
</RadioButton>
<RadioButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/r2"
android:text="Radio Button 2"
android:textSize="20dp"
android:textStyle="bold"
android:layout_below="@id/r1">
</RadioButton>
<TextView
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton inside RadioGroup"
android:textStyle="bold"
android:textSize="29dp"
android:layout_below="@id/r2"
android:layout_marginTop="20dp"

android:layout_centerHorizontal="true">

</TextView>
<RadioGroup
android:id="@+id/rg"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@id/t2">
<RadioButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/r3"
android:text="Male"
android:layout_marginTop="10dp"
android:textSize="25dp"
android:textStyle="bold"
android:layout_below="@id/rg"/>
<RadioButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/r4"
android:text="Female"
android:textSize="25dp"
android:textStyle="bold"
android:layout_below="@id/r3"
android:layout_marginTop="7dp"/>

</RadioGroup>
<Button
android:id="@+id/b5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SHOW SELECTED"
android:textSize="15sp"
android:textStyle="bold"
android:layout_centerHorizontal="true"
android:layout_below="@id/rg">

</Button>

</RelativeLayout>

//java file
package com.example.sh;

import android.content.Context;
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 {


Button b1;
RadioGroup rog;
RadioButton r1,r2,r3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
b1 = findViewById(R.id.b5);
rog = findViewById(R.id.rg);
r1 = findViewById(R.id.r1);
r2 = findViewById(R.id.r2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int a = rog.getCheckedRadioButtonId();
r3 = findViewById(a);
String str = "Selected RadioButton";
if (r1.isChecked()) {
str = str + "" + r1.getText();

}
if (r2.isChecked()) {
str = str + "" + r2.getText();
;
}
str = str + "" + r3.getText();

Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();


}

});
}}
OUTPUT:

You might also like