0% found this document useful (0 votes)
14 views2 pages

Practical No 12

The document contains an XML layout for an Android application featuring a RadioGroup with three RadioButtons and a Button to submit the selection. The MainActivity class handles the logic to display the selected option or prompt the user to select one. It initializes UI components and sets a click listener on the submit button to update a TextView with the selected option's text.

Uploaded by

Vaman Kulkarni
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)
14 views2 pages

Practical No 12

The document contains an XML layout for an Android application featuring a RadioGroup with three RadioButtons and a Button to submit the selection. The MainActivity class handles the logic to display the selected option or prompt the user to select one. It initializes UI components and sets a click listener on the submit button to update a TextView with the selected option's text.

Uploaded by

Vaman Kulkarni
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/ 2

Practical No 12

<?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:gravity="center"
android:padding="16dp">
<!-- RadioGroup to group RadioButtons -->
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="20dp">
<!-- First RadioButton -->
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/option_1" />
<!-- Second RadioButton -->
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/option_2" />
<!-- Third RadioButton -->
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/option_3" />
</RadioGroup>
<!-- Button to submit the selection -->
<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/submit" />
<!-- TextView to display the result -->
<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="@android:color/black"
android:layout_marginTop="20dp"/>
</LinearLayout>

Main.java

package com.example.radio_buttonradio_group;

import android.os.Bundle;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {

private RadioGroup radioGroup;


private TextView resultTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize UI components
radioGroup = findViewById(R.id.radioGroup);
Button submitButton = findViewById(R.id.submitButton);
resultTextView = findViewById(R.id.resultTextView);
// Set the button click listener
submitButton.setOnClickListener(v -> {
// Get the selected radio button's ID
int selectedId = radioGroup.getCheckedRadioButtonId();
// Check if a radio button is selected
if (selectedId != -1) {
// Find the radio button by ID
RadioButton selectedRadioButton = findViewById(selectedId);
String selectedOption = selectedRadioButton.getText().toString();
resultTextView.setText("You selected: " + selectedOption);
} else {
resultTextView.setText("Please select an option.");
} }); }}

OUTPUT:

You might also like