Radio buttons are used to select one single option only from a group of options. For example, if we want to know wheather the user is male or female, then we have to use Radio Buttons. If we want a user to select only one option from the set, Radio Buttons are the way to do it.

A Radio Group in Android

To use a Radio Button we should use a Radio Group. We have to add radio buttons in one group so that we can limit the option selection to a single value only.

What are we going to learn from this Example?

In this example, we will learn about the Radio Button, Radio Group, and the event listener. You can learn more about the event listener from the Android buttons tutorial.

In this example application, we have used one TextView, two RadioButtons, and one RadioGroup. From the screen shots below, you can see that we have a small application that will ask you “What would you like to have, tea or coffee?”. This means the user can only give one answer. That’s the basics of it.


Below you can find the screen shots of the Android button example application.

radio button

Whenever the user clicks on a radio button, he will be presented with a pop up.

radio toast

How to add Radio Buttons to the layout file.

You can use a XML layout file to add Radio Buttons. Below you can find the code needed to add Radio Buttons to your activity.

When you want to set the default selection Radio Button among the group, you can use the code below.

Once you create a new Android project, you can add the above lines of code inside your activity_main.xml file or inside a custom XML file you have created for your Android button application.

Building a Layout for the Radio Button Example

Now we are going to build a complete layout for our Android application. Let’s design our activity screen. Our activity screen consists of the following views:

  1. One TextView which will act as a label (“What would you like to have?”).
  2. Two RadioButtons, one for Tea and one for Coffee.
  3. One RadioGroup to mange the Radio Buttons in a single group so that the user can only select one option.

Below you can find the complete layout code for our Android Radio Button example.

Listing 1: create the activity_main.xml layout file.

How to access the Radio Button and the Radio Group in Android using a java class.

A Radio Button class in Android can be imported by using the package below.

A Radio Group class in Android can be imported by using the package below.

You can create a new Radio Button reference by using the code snippet below. We get the reference of a Radio Button by using its ID which we have created in our Android XML file

The Change and Click Events for the Radio Button.

To handle a change event for an Android Radio Button, use OnCheckedChangeListener which is given below.

Now let’s look at the main activity for our Android Radio Button example application.

Listing 1: Create file.

From the above code, you can see that we have Toast set to onCheckedChange on the radio group. Then we check which Radio Button is selected and according to that, we setup text in a popup.


In this Android Radio Button example, we learned about adding Radio Buttons to an Android activity. We have also seen how to make a specific Radio Button the default selection and how the onCheckedChangeListener works. Using all this information, we have made a small application for Android Radio Buttons.

If you have any suggestions or want to ask something, please do so in the comments section below. Thank you for reading. All the best.