---
title: "Radio Buttons control in Android"  
description: "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 Ra"  
author: "Chris Anderson"  
published: 2011-10-20  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/787/radio-buttons-control-in-android  
category: "android"  
tags: ["android"]  
reading_time: 3 minutes  

---

# Radio Buttons control in Android

In this [article](https://yourviews.mindstick.com/view/81489/kashmir-now-after-an-year-of-abrogation-of-article-370), I am going to [explain](https://www.mindstick.com/forum/159699/what-is-a-compilation-error-in-dot-net-core-explain-with-an-example) how to create two mutually-exclusive (in a group) radio buttons ([enabling](https://yourviews.mindstick.com/view/86018/ai-and-5g-enabling-the-internet-of-things-iot) one disables the other), using the RadioGroup and [RadioButton](https://www.mindstick.com/blog/233/get-checked-radiobutton-using-javascript) widgets. When either [radio button](https://www.mindstick.com/articles/12743/radio-button-in-android) is pressed, a toast message will be displayed.\

· Start a new [project](https://yourviews.mindstick.com/story/1788/things-to-ensure-your-construction-project-is-successful), named RadioButtonDemo.

· Open the **res/layout/main.xml** file and [add two](https://www.mindstick.com/forum/480/add-two-number) RadioButtons, nested in a RadioGroup (inside the LinearLayout):

```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">

    <RadioGroup android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:orientation="vertical" >
               <RadioButton android:id="@+id/radio_male"
                             android:layout_width="wrap_content"
                             android:layout_height="wrap_content"
                             android:text="Male"/>
               <RadioButton android:id="@+id/radio_female"
                             android:layout_width="wrap_content"
                             android:layout_height="wrap_content"
                             android:text="Female"/>
    </RadioGroup>
</LinearLayout>
```

· Now when each RadioButton is selected, you need a **View.[OnClickListener](https://www.mindstick.com/forum/33726/inside-onclicklistener-i-cannot-access-a-lot-of-things-how-to-approach)**. so add the following code in the [Activity](https://www.mindstick.com/forum/23119/how-to-save-activity-state-in-android):

```
import android.app.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(),
                                                           Toast.
                                                          Toast.LENGTH_SHORT).show();
              }
       };

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         final RadioButton radioMale=(RadioButton)findViewById(R.id.radio_male);
         final RadioButton radioFemale=(RadioButton)findViewById(R.id.radio_female);
         radioMale.setOnClickListener(radioListener);
         radioFemale.setOnClickListener(radioListener);
    }
}
```

· Run the [application](https://www.mindstick.com/articles/12824/calculator-application-in-android)\

##### Your output is something like below:

![Radio Buttons control in Android](https://www.mindstick.com/mindstickarticle/395b7040-effc-40a2-818b-39c6b5ced446/images/452f4b13-6217-4e69-9150-b320e1a94305.png)

When any radio button is selected by the user, the radiobutton text will display in the toast message.

---

Original Source: https://www.mindstick.com/articles/787/radio-buttons-control-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
