What Is Radio Button And How To Add Radio Buttons

Radio Button – Introduction

Radio Button –

Create activity_radio_button File in XML

<?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”
android:gravity=”center”
tools:context=”.radioButtonActivity”>

<TextView
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Please Select Your Gender”
android:textSize=”20sp”
android:gravity=”center”
android:textStyle=”bold”/>

<RadioGroup
android:id=”@+id/radioGroup1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginTop=”30dp” >

<RadioButton
android:id=”@+id/radioMale”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Male”
android:checked=”true”/>

<RadioButton
android:id=”@+id/radioFemale”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Female”
android:checked=”false”/>

<RadioButton
android:id=”@+id/radioOther”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Other”
android:checked=”false”/>

</RadioGroup>

<TextView
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_marginTop=”30dp”
android:text=”Other Example”
android:textSize=”20sp”
android:textStyle=”bold”
android:gravity=”center”/>

<RadioGroup
android:id=”@+id/radioGroup2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginTop=”20dp”>

<RadioButton
android:id=”@+id/radioBoy”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Boy”
android:checked=”true”/>

<RadioButton
android:id=”@+id/radioGirl”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Girl”
android:checked=”false”/>

</RadioGroup>

</LinearLayout>

Create radioButtonActivity File in Java

package com.eiheducation.demo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class radioButtonActivity extends AppCompatActivity {

RadioGroup radioGroup1, radioGroup2; // Radio Group Object
RadioButton radioMale, radioFemal, radioOther, radioBoy, radioGirl; // Radio Button Objects

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_button);

// We need to find our elements by IDs

radioGroup1 = findViewById(R.id.radioGroup1);
radioGroup2 = findViewById(R.id.radioGroup2);
radioMale = findViewById(R.id.radioMale);
radioFemal = findViewById(R.id.radioFemale);
radioOther = findViewById(R.id.radioOther);
radioBoy = findViewById(R.id.radioBoy);
radioGirl = findViewById(R.id.radioGirl);

// Button Code

radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radioMale:
Toast.makeText(radioButtonActivity.this, “Male”, Toast.LENGTH_SHORT).show();
break;
case R.id.radioFemale:
Toast.makeText(radioButtonActivity.this, “Female”, Toast.LENGTH_SHORT).show();
break;
case R.id.radioOther:
Toast.makeText(radioButtonActivity.this, “Other”, Toast.LENGTH_SHORT).show();
break;
}
}
});

radioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radioBoy:
Toast.makeText(radioButtonActivity.this, “Boy”, Toast.LENGTH_SHORT).show();
break;
case R.id.radioGirl:
Toast.makeText(radioButtonActivity.this, “Girl”, Toast.LENGTH_SHORT).show();
break;
}
}
});
}
}

Android Manifest File

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.eiheducation.demo”>

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/Theme.EIHeducationDemo”>
<activity
android:name=”.MainActivity”
android:label=”@string/app_name”
android:theme=”@style/Theme.EIHeducationDemo.NoActionBar”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.radioButtonActivity” />
</application>

</manifest>

Radio Button – Output

Grammarly – Best Free Grammar checker | Spelling checker | Mistake-free document writer for Laptop/Desktop
Online Surveys – Earn Real Money by Online Surveys
Kids Books – 10+ books for Kids | Age group 2 – 10 | Improve knowledge & Productivity

Leave a Reply

Your email address will not be published.