0% found this document useful (0 votes)
54 views5 pages

Radiobuttonprg

The document contains XML and Java code for an Android application that implements radio buttons. The XML code defines the layout of radio buttons not in a radio group and radio buttons within a radio group. The Java code finds the radio button views and handles checking which radio button is selected when a button is clicked, displaying it in a toast message.

Uploaded by

Sneha Dhavale
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)
54 views5 pages

Radiobuttonprg

The document contains XML and Java code for an Android application that implements radio buttons. The XML code defines the layout of radio buttons not in a radio group and radio buttons within a radio group. The Java code finds the radio button views and handles checking which radio button is selected when a button is clicked, displaying it in a toast message.

Uploaded by

Sneha Dhavale
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/ 5

Xml code

<?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="20dp"
android:textColor="#4B0082"
/>
<RadioButton
android:id="@+id/radio1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Radio Button 1"
android:textSize="20dp"
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="20dp"
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:textColor="#4B0082"
android:textSize="20dp" />
<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="20dp"
android:textStyle="bold" />
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:textSize="20dp"
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>
Java code
package com.example.radiobutton;
import android.os.Bundle;
import android.view.View;
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, 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();
}
}

Output:-

You might also like