In this article, I am going to explain how to create two mutually-exclusive (in a group) radio buttons (enabling one disables the other), using the RadioGroup and RadioButton widgets. When either radio button is pressed, a toast message will be displayed.

·         Start a new project, named RadioButtonDemo.

·         Open the res/layout/main.xml file and add two RadioButtons, nested in a RadioGroup (inside the LinearLayout):

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

<LinearLayout xmlns:android=""

    <RadioGroup android:layout_width="fill_parent"
                 android:orientation="vertical" >
               <RadioButton android:id="@+id/radio_male"
               <RadioButton android:id="@+id/radio_female"


·     Now when each RadioButton is selected, you need a View.OnClickListener. so add the following code in the Activity:


import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.Toast;

public class RadioButtonActivity extends Activity {

        private OnClickListener radioListener= new OnClickListener() {
               public void onClick(View v) {
                     RadioButton rb=(RadioButton)v;
                     Toast.makeText(RadioButtonActivity.this, rb.getText(),

     public void onCreate(Bundle savedInstanceState) {
         final RadioButton radioMale=(RadioButton)findViewById(;
         final RadioButton radioFemale=(RadioButton)findViewById(;

·         Run the application

Your output is something like below:

Radio Buttons control in Android

When any radio button is selected by the user, the radiobutton text will display in the toast message.

  Modified On Mar-23-2018 02:07:21 AM

Leave Comment