0% found this document useful (0 votes)
10 views4 pages

Practical No-12

The document contains a Java implementation of an Android application that utilizes RadioButtons and a RadioGroup to allow users to select options. It includes a button that, when clicked, displays the selected options using a Toast message. The layout is defined in XML, featuring RadioButtons both inside and outside of a RadioGroup, along with a button to show the selected choices.

Uploaded by

ratnadipkakade3
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)
10 views4 pages

Practical No-12

The document contains a Java implementation of an Android application that utilizes RadioButtons and a RadioGroup to allow users to select options. It includes a button that, when clicked, displays the selected options using a Toast message. The layout is defined in XML, featuring RadioButtons both inside and outside of a RadioGroup, along with a button to show the selected choices.

Uploaded by

ratnadipkakade3
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/ 4

Practical No-12

MainActivity.java:
package com.example.radiobuttonapp;

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.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

RadioButton radio1, radio2, radio3, radio4;


RadioGroup radioGroup;
Button btnShowSelected;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initialize RadioButtons without RadioGroup


radio1 = findViewById(R.id.radio1);
radio2 = findViewById(R.id.radio2);

// Initialize RadioButtons inside RadioGroup


radioGroup = findViewById(R.id.radioGroup);
radio3 = findViewById(R.id.radio3);
radio4 = findViewById(R.id.radio4);

btnShowSelected = findViewById(R.id.btnShowSelected);

// Button Click Listener to display selected


RadioButton
btnShowSelected.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuilder selectedText = new
StringBuilder("Selected: ");

// Checking selection of RadioButtons WITHOUT


RadioGroup
if (radio1.isChecked()) selectedText.append("\
n").append(radio1.getText());
if (radio2.isChecked()) selectedText.append("\
n").append(radio2.getText());
// Checking selection of RadioButtons WITH
RadioGroup
int selectedId =
radioGroup.getCheckedRadioButtonId();
if (selectedId != -1) {
RadioButton selectedRadio =
findViewById(selectedId);
selectedText.append("\
n").append(selectedRadio.getText());
}

if (selectedText.toString().equals("Selected:
")) {
Toast.makeText(MainActivity.this, "No
options selected", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this,
selectedText.toString(), Toast.LENGTH_SHORT).show();
}
}
});
}
}

activity_main.xml:
<?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:padding="16dp">

<!-- RadioButtons WITHOUT RadioGroup -->


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Without RadioGroup"
android:textStyle="bold"
android:textSize="18sp"
android:paddingBottom="10dp"/>

<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 1" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 2" />

<!-- RadioButtons WITH RadioGroup -->


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="With RadioGroup"
android:textStyle="bold"
android:textSize="18sp"
android:paddingTop="20dp"
android:paddingBottom="10dp"/>

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

<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio" />

<RadioButton
android:id="@+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 4" />
</RadioGroup>

<Button
android:id="@+id/btnShowSelected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Selected"
android:layout_marginTop="20dp"/>

</LinearLayout>
Output:

You might also like