Android RadioButton

In this tutorial, we are going to discuss about android radio button widget. We will also learn about different attributes of radio button that are used to customise this widget.

Android RadioButton can be defined as below –

"RadioButton is a two states button which is either checked or unchecked. If a single radio button is unchecked, we can click it to make the radio button checked. RadioButton is usually used with RadioGroup."

RadioButtons are normally used in the RadioGroup. Once a radio button is checked, it cannot be marked as user unchecked. When there are many radio buttons in Radio Group, Checking one radio button unchecks all other radio buttons.





S. No. XML Attributes Description
1 android:clickable Set true/false depending on whether you want to make this view clickable. Set true when you want to make this view clickable.
2 android:drawableBottom Sets drawable to show at bottom of the text.
3 android:drawableEnd Sets drawable to show at end of the text.
4 android:drawableLeft Sets drawable to show at left of the text.
5 android:drawablePadding Sets padding of the drawable.
6 android:backgroundTint Sets tint to the background of View.

Some of the popular attributes of RadioButton inherited from Compound Button are –

S. No. XML Attributes Description
1 android:button Drawable to be used for button graphic.
2 android:buttonTint Sets tint to the button.

Some of the popular attributes of RadioButton inherited from View are –

S. No. XML Attributes Description
1 android:id This is unique id of the RadioButton to uniquely identify the RadioButton.
2 android:tooltipText Define the text to be show as tooltip when cursor is hovered on it.
3 android:visibility Sets whether to show/hide this view.
4 android:onClick Define what to do when view is clicked.
5 android:background Sets background.  
6 android:padding Sets padding of View.
7 android:alpha Defines alpha in view.





At first, we will create android application. Then, we will use RadioButton in android application.

Follow the steps below to create new project. Please ignore steps if you've already created a new application.

S. No. Steps
1 Open Android Studio.
2 Go to File => New => New Project. Write application name as RadioButton. Then, click next button.
3 Select minimum SDK you need. However, we have selected 14 as minimum SDK. Then, click next button
4 Then, select Empty Activity => click next => click finish.
5 If you've followed above process correctly, you will get a newly created project successfully. However, you can also visit tutorial to Create a New Project to know steps in detail.

Now, we will modify xml and java file to use RadioButton in the application.

Open res/values/strings.xml file. Then, add below code into it.

<string name="app_name">RadioButton</string>
<string name="radio_Male">Male</string>
<string name="radio_Female">Female</string>

Other values folder have not been changed. So, we're not going to mention it here.

Open res/layout/activity_main.xml file. Then, add below code into it.

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






As mentioned earlier, it is not possible to uncheck the RadioButton by clicking it once it is checked. We use it with RadioGroup and other RadioButtons. When several radio buttons are used in a radio group, clicking on any radio button checks it and unchecks all other radio buttons. Thus, at any time only one radio button can be selected.

We used two RadioButtons inside a RadioGroup in the activity_main.xml file. One radio button represents  a male gender, another represents a female. So, if you want to select a gender, you can select either a male or a female.

Radio button is used in similar situations as if you only want to select one option out of many options.





Open src/main/java/com.ukacademe.radiobutton/ file. Then, add below code into it.

package com.ukacademe.radiobutton;

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

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

        RadioGroup radioGroup = findViewById(;
        if (radioGroup != null) {
            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    String text = "You Selected: ";
                    text += ( == checkedId) ? "Male" : "Female";
                    Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();


We access RadioButtons defined in xml file in the MainActivity file. Then, when selection changes in the RadioGroup, i.e., we show a toast message when any radio button is selected, we will display a toast message.

Since the file of AndroidManifest.xml is very important in any android application. We are also going to see the content of this file.

Code inside src/main/AndroidManifest.xml file is as below &ndash

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""

<application android:allowBackup="true"
<activity android:name=".MainActivity">
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


When you run the application, you will get output as shown below.

UK Academe-RadioButton-Output