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.

Output

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.

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

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"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:gravity="center">

	<RadioGroup
		android:id="@+id/radioGroup"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:paddingLeft="20dp">

		<RadioButton
			android:id="@+id/radioMale"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:padding="10dp"
			android:text="Male"/>

		<RadioButton
			android:id="@+id/radioFemale"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:padding="10dp"
			android:text="Female"/>

	</RadioGroup>

</LinearLayout>

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/MainActivity.java file. Then, add below code into it.

package com.tutorialwing.radiobutton;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final RadioGroup radioGroup = findViewById(R.id.radioGroup);
		if (radioGroup != null) {
			radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(RadioGroup group, int checkedId) {
					String text = "You selected: ";
					text += (R.id.radioMale == 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.

AndroidManifest.xml

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

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.radiobutton"
		  xmlns:android="http://schemas.android.com/apk/res/android">

	<application
		android:allowBackup="true"
		android:icon="@mipmap/ic_launcher"
		android:label="@string/app_name"
		android:roundIcon="@mipmap/ic_launcher_round"
		android:supportsRtl="true"
		android:theme="@style/AppTheme">
		<activity android:name=".MainActivity">
			<intent-filter>
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>
			</intent-filter>
		</activity>
	</application>

</manifest>

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

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

Leave a Reply