0% found this document useful (0 votes)
20 views

8) Write A Program To Show Functions

The document shows the XML layout code and Java code to implement radio buttons and a radio group. It defines radio buttons outside and inside a radio group. It also includes a button click listener to toast the selected radio option.

Uploaded by

ethicalninja7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

8) Write A Program To Show Functions

The document shows the XML layout code and Java code to implement radio buttons and a radio group. It defines radio buttons outside and inside a radio group. It also includes a button click listener to toast the selected radio option.

Uploaded by

ethicalninja7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

8) Write a program to show functionality of radio group and radio buttons to toast

selected gender.
exp-12 Ans: Xml code

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="248dp"
android:layout_height="41dp"
android:text="Single radio buttons"
android:textAlignment="center"
android:textColor="#9C27B0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.091" />

<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.27"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.218" />

<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.27"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.31" />

<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="258dp"
android:layout_height="135dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.516"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.545">

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Radio button inside radiogroup"
android:textAlignment="center"
android:textColor="#9C27B0" />

<RadioButton
android:id="@+id/radioButton3"
android:layout_width="match_parent"
android:layout_height="48dp"
android:text="male" />

<RadioButton
android:id="@+id/radioButton4"
android:layout_width="match_parent"
android:layout_height="46dp"
android:text="female" />

</RadioGroup>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show selected"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.781" />

</androidx.constraintlayout.widget.ConstraintLayout>

___________________________________________________________________________________
_________________

*Java Code

package com.example.radiobutton;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


RadioButton rb1,rb2,rb3;
RadioGroup rg1; Button b1;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rb1=findViewById(R.id.radioButton);
rb2=findViewById(R.id.radioButton2);
rg1=findViewById(R.id.radioGroup);
b1=findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener()
{
@Override public void onClick(View view)
{
if (rb1.isChecked())
{
Toast.makeText(MainActivity.this,rb1.getText().toString()+" is
clicked",Toast.LENGTH_SHORT).show();
}
else if (rb2.isChecked())
{
Toast.makeText(MainActivity.this,rb2.getText().toString()+" is
clicked",Toast.LENGTH_SHORT).show();
}
int id=rg1.getCheckedRadioButtonId();
rb3=findViewById(id);
Toast.makeText(MainActivity.this,rb3.getText().toString()+" is
clicked",Toast.LENGTH_SHORT).show(); } });
}
}

You might also like