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

Practical 12

The document describes a program that implements radio buttons and a radio group in an Android application. It defines the layout using XML with three radio buttons within a radio group. It then implements the Java code to get references to the radio group and buttons, set an onCheckedChange listener on the group to detect button selection changes, and display a toast with the selected button text.

Uploaded by

swapnil kale
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)
41 views2 pages

Practical 12

The document describes a program that implements radio buttons and a radio group in an Android application. It defines the layout using XML with three radio buttons within a radio group. It then implements the Java code to get references to the radio group and buttons, set an onCheckedChange listener on the group to detect button selection changes, and display a toast with the selected button text.

Uploaded by

swapnil kale
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 12

PROGRAM TO IMPLEMENT RADIO BUTTON AND RADIO GROUP

The layout of the MainActivity is defined as follows. activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

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

<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 1"
android:checked="true"/>

<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 2"/>

<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Option 3"/>

</RadioGroup>

</RelativeLayout>

The MainActivity.java is defined below.

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

RadioGroup radioGroup;
RadioButton radioButton1, radioButton2, radioButton3;

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

// Get the radio group view and all the radio buttons
radioGroup = findViewById(R.id.radioGroup);
radioButton1 = findViewById(R.id.radioButton1);
radioButton2 = findViewById(R.id.radioButton2);
radioButton3 = findViewById(R.id.radioButton3);

// Set a listener for the radio group changes


radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// Get the selected radio button
RadioButton selectedRadioButton = findViewById(checkedId);

// Display a toast message with the selected radio button text


Toast.makeText(getApplicationContext(), "You have selected: " +
selectedRadioButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}

You might also like