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

RadioButtonExample

The document provides an example of implementing RadioButtons in an Android application using XML and Java. It includes a layout with single RadioButtons and a RadioGroup containing male and female options, along with a button to display the selected options. The Java code handles the button click event to show a Toast message with the selected RadioButton values.

Uploaded by

ankitasurya663
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)
12 views3 pages

RadioButtonExample

The document provides an example of implementing RadioButtons in an Android application using XML and Java. It includes a layout with single RadioButtons and a RadioGroup containing male and female options, along with a button to display the selected options. The Java code handles the button click event to show a Toast message with the selected RadioButton values.

Uploaded by

ankitasurya663
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

Android RadioButton Example

XML Layout (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">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Single Radio Buttons"
android:textSize="18sp"
android:textStyle="bold"
android:paddingBottom="8dp"/>

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

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio button inside RadioGroup"
android:textSize="18sp"
android:textStyle="bold"
android:paddingTop="16dp"
android:paddingBottom="8dp"/>

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

<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"/>
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"/>
</RadioGroup>

<Button
android:id="@+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SHOW SELECTED"
android:layout_marginTop="16dp"/>

</LinearLayout>

Java Code (MainActivity.java)

package com.example.radiobuttonexample;

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 radioButton1, radioButton2, radioMale, radioFemale;


RadioGroup radioGroup;
Button btnShow;

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

radioButton1 = findViewById(R.id.radioButton1);
radioButton2 = findViewById(R.id.radioButton2);
radioMale = findViewById(R.id.radioMale);
radioFemale = findViewById(R.id.radioFemale);
radioGroup = findViewById(R.id.radioGroup);
btnShow = findViewById(R.id.btnShow);

btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String selectedText = "";
if (radioButton1.isChecked()) {
selectedText = "Selected: Radio Button 1";
} else if (radioButton2.isChecked()) {
selectedText = "Selected: Radio Button 2";
}

int selectedId = radioGroup.getCheckedRadioButtonId();


if (selectedId != -1) {
RadioButton selectedRadioButton = findViewById(selectedId);
selectedText += "\nSelected: " +
selectedRadioButton.getText().toString();
}

Toast.makeText(MainActivity.this, selectedText,
Toast.LENGTH_SHORT).show();
}
});
}
}

You might also like