Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Q.WAP to show the following output.

First two radio button are without using radiogroup and


next two radio buttons are using radio group.
XML File:
<?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"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textAlignment="center"
android:text="PROGRAM FOR RADIOBUTTON"
android:textSize="30dp"
android:textStyle="bold"/>

<RadioButton
android:id="@+id/r1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 1"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp"
android:padding="15dp"
android:textSize="25dp"/>
<RadioButton
android:id="@+id/r2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Radio Button 2"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:padding="15dp"
android:textSize="25dp"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textAlignment="center"
android:text="Radiobutton inside RadioGroup"
android:textSize="30dp"
android:textStyle="bold"/>

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/r3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp"
android:padding="15dp"
android:textSize="25dp"/>

<RadioButton
android:id="@+id/r4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp"
android:padding="15dp"
android:textSize="25dp"/>

</RadioGroup>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="SHOW SELECTED"
android:onClick="onRadioClick"
android:textColor="@color/white"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:textSize="20dp"/>
</LinearLayout>

JAVA File:
package com.example.rbutton;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


RadioButton r1, r2, r3,r4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
r1=(RadioButton)findViewById(R.id.r1);
r2=(RadioButton)findViewById(R.id.r2);
r3=(RadioButton)findViewById(R.id.r3);
r4=(RadioButton)findViewById(R.id.r4);
}
public void onRadioClick (View v)
{
String result="";
if(r1.isChecked())
{
result+=r1.getText().toString()+"\n";
}
if(r2.isChecked())
{
result+=r2.getText().toString()+"\n";
}
if(r3.isChecked())
{
result+=r3.getText().toString()+"\n";
}
if(r4.isChecked())
{
result+=r4.getText().toString()+"\n";
}

if(result!=null)
{
result=result.substring(0,result.length()-1);
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
}
}
}

Output:

You might also like