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

Pra 12 EX

The document contains an XML layout and Java code for an Android application featuring radio buttons. It includes a single radio button section and a radio group with male and female options, along with a button to display the selected options. The Java code handles the logic to show a toast message with the selected radio button values when the button is clicked.

Uploaded by

sammedmohare465
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)
20 views3 pages

Pra 12 EX

The document contains an XML layout and Java code for an Android application featuring radio buttons. It includes a single radio button section and a radio group with male and female options, along with a button to display the selected options. The Java code handles the logic to show a toast message with the selected radio button values when the button is clicked.

Uploaded by

sammedmohare465
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

Pra:12

Ex:Q.1

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout 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"
android:orientation="vertical"
android:padding="20dp"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Single Radio Button"
android:textSize="20sp" />

<RadioButton
android:id="@+id/radio1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Radio Button 1"
android:textSize="18dp"
android:textStyle="bold"/>

<RadioButton
android:id="@+id/radio2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Radio Button 2"
android:textSize="18dp"
android:textStyle="bold" />

<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@android:color/black"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"/>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Radio button inside RadioGroup"
android:textSize="20sp" />

<RadioGroup android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:textSize="18dp"
android:textStyle="bold" />

<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:textSize="18dp"
android:textStyle="bold" />

</RadioGroup>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Show Selected"
android:onClick="showSelected"/>

</LinearLayout>
package com.example.radiobuttonapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

RadioButton radio1, radio2, radioMale, radioFemale;


RadioGroup radioGroup;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

radio1 = findViewById(R.id.radio1);
radio2 = findViewById(R.id.radio2);
radioMale = findViewById(R.id.radioMale);
radioFemale = findViewById(R.id.radioFemale);
radioGroup = findViewById(R.id.radioGroup);
}

public void showSelected(View view) {


String selected = "Selected radio buttons:\n";

if(radio1.isChecked())
selected += "Radio Button 1\n";

if(radio2.isChecked())
selected += "Radio Button 2\n";

RadioButton selectedRadio = findViewById(radioGroup.getCheckedRadioButtonId());


selected += selectedRadio.getText().toString();
Toast.makeText(MainActivity.this, selected, Toast.LENGTH_SHORT).show();
}
}

You might also like