Android Radio Button Tutorial With Example


Hello Readers! In this post, 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.


Tutorialwing Android Radio Button Output Android Radio Button Tutorial Use Android Radio button widget in android project

Tutorialwing Android Radio Button Output

Getting Started

Android Radio Button can be defined as below –

An Android Radio Button is a two states button that can either be checked or unchecked. When the radio button is in unchecked state, it can be pressed or clicked to check it. Unlike Checkbox, it can not be unchecked (by clicking it) once it is checked.

We normally use Radio Buttons in Radio Group. When there are many radio buttons in Radio Group, Checking one radio button unchecks all other radio buttons.

Attributes of Android Radio Button Widget

Attributes of Radio Button are inherited from Compound Button, TextView and View. Some of the popular attributes of Radio Button inherited from TextView are –

Sr. XML Attributes Description
1 android:backgroundTint Sets tint to the background of View
2 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.
3 android:drawableBottom Sets drawable to show at bottom of the text
4 android:drawableEnd Sets drawable to show at end of the text
5 android:drawableLeft Sets drawable to show at left of the text
6 android:drawablePadding Sets padding of the drawable.

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

Sr. 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 Radio Button inherited from View are –

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

Example of Android Radio Button Widget

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

1. Creating New Project

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

Step Description
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 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail.

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

2. Modify Values folder

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

	<string name="app_name">RadioButton</string>

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

3. Use Radio Button Widget in xml file

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

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






As mentioned earlier, Radio Button can not be unchecked, by clicking it, once it is checked. So, we use it with Radio Group and other radio buttons. When several radio buttons are used in a radio group, clicking on any radio button checks it and unchecks all other radio buttons. Thus, only one radio button can be selected at any time.

In activity_main.xml file, we have used two radio buttons inside a radio group. One radio button represents gender male, other represents female. So, If you want to select gender, you can either select male or female.

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

4. Access Radio Button Widget in java file

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

package com.tutorialwing.radiobutton;

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

public class MainActivity extends AppCompatActivity {

	protected void onCreate(Bundle savedInstanceState) {

		final 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();

In MainActivity file, we are accessing radio buttons defined in xml file. Then we are showing a toast message when selection changes in radio group i.e. we are showing a toast message when any radio button is selected.

Since AndroidManifest.xml file 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 –

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.radiobutton"

		<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 above.

That’s end of our tutorial on Android Radio Button.