Write an Android program to select gender using radio button.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >
<RadioGroup
android:id="@+id/radiogrp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Your Gender" />
<RadioButton
android:id="@+id/radiomale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="@+id/radiofemale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Female" />
<RadioButton
android:id="@+id/radiononbin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Non binary" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />
</RadioGroup>
</RelativeLayout>
MainActivity.java
package com.teachics.a5radiobutton;
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 {
Button but;
RadioGroup rgroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but=(Button) findViewById(R.id.button);
rgroup=(RadioGroup) findViewById(R.id.radiogrp);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selected=rgroup.getCheckedRadioButtonId();
RadioButton radio=(RadioButton) findViewById(selected);
Toast.makeText(MainActivity.this,"You selected : "+radio.getText(),Toast.LENGTH_LONG).show();
}
});
}
}
Output


