pract12
pract12
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;
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();
}
}
});
}
}