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

pract12

The document contains XML and Java code for an Android application that implements radio buttons within a user interface. It includes a layout with text views, radio buttons, and a button to show the selected option. The Java code handles button clicks to display a toast message indicating which radio button is selected or if none are checked.

Uploaded by

kishorigavde001
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)
2 views

pract12

The document contains XML and Java code for an Android application that implements radio buttons within a user interface. It includes a layout with text views, radio buttons, and a button to show the selected option. The Java code handles button clicks to display a toast message indicating which radio button is selected or if none are checked.

Uploaded by

kishorigavde001
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/ 3

Practical no :12

XMLcode
<?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:id="@+id/main"
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:text="single radio button"
android:textSize ="15dp"/>
<RadioButton
android:id="@+id/rd1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="radio button1"
android:checked="false"
android:textSize="15dp"/>
<RadioButton
android:id="@+id/rd2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="radio button2"
android:checked="false"
android:textSize="15dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="radio button inside radiogroup"
android:layout_marginTop="10dp"
android:gravity="center"
android:textStyle="bold"
android:textSize ="15dp"/>
<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:clickable="false"
android:orientation="vertical"/>
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="male"
android:checked="false"
android:textSize="15dp"/>

<RadioButton
android:id="@+id/fm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="female"
android:checked="false"
android:textSize="15dp"/>

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show selected"/>
</LinearLayout>

Javacode
package com.example.radiobutton;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

RadioButton r1,r2,male,female;
Button btn1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
r1 = (RadioButton)findViewById(R.id.radioButtonNo1);
r1 = (RadioButton)findViewById(R.id.radioButtonNo2);
male = (RadioButton)findViewById(R.id.grpmale);
female = (RadioButton)findViewById(R.id.grpfemale);
btn1=(Button)findViewById(R.id.buttonNo1);

btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (r1.isChecked()) {
Toast.makeText(MainActivity.this, "Radio Button1 is checked",
Toast.LENGTH_SHORT).show();
} else if (r2.isChecked()) {
Toast.makeText(MainActivity.this, "Radio Button2 is checked",
Toast.LENGTH_SHORT).show();
} else if (male.isChecked()) {
Toast.makeText(MainActivity.this, "Male is checked",
Toast.LENGTH_SHORT).show();
} else if (female.isChecked()) {
Toast.makeText(MainActivity.this, "Female is checked",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Nothing is checked",
Toast.LENGTH_SHORT).show();
}
}
});
}
}

You might also like