Android Radio Group Tutorial With Example

Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.

Hello Readers! In this post, we are going to learn about how to use android radio group in any android application. We will also go through different attributes of android radio Group that can be used to customise it.

Output

Tutorialwing Android Radio Group Output Android Radio Group Widget tutorial with example

Tutorialwing Android Radio Group Output

Getting Started

Android Radio Group can be defined as below –
Android RadioGroup is a class used to create a multiple-exclusion scope for a set of radio buttons. When several radioButtons are used in a RadioGroup, selecting one radioButton unselects other radioButtons within same group.

Note: – Initially, all radioButtons are unchecked. Checked/Unchecked states of radioButtons are identified by using their unique ids.

Attributes of Android Radio Group Widget

Some of the popular attributes of android Radio group inherited form LinearLayout are –

Sr. XML Attributes Description
1 android:baselineAligned Specifies whether to align radioGroup’s children baselines
2 android:divider Drawable to be used as a vertical divider between buttons
3 android:gravity Specifies how an object will be aligned within this layout
4 android:orientation It defines how the object should be aligned within this layout. For example, horizontal or vertical etc.
5 android:weightSum Defines the maximum weight sum

Some of the popular attributes of android radio group inherited from ViewGroup are –

Sr. XML Attributes Description
1 android:animationCache Defines whether layout animations should create a drawing cache for their children
2 android:clipChildren Defines whether a child is limited to draw inside of its bounds or not.
3 android:layoutMode Defines the layout mode of this ViewGroup.

Some of the popular attributes of android radio group inherited from View are –

Sr. XML Attributes Description
1 android:alpha Sets alpha of the view
2 android:background Sets background of the view
3 android:backgroundTint Sets background tint
4 android:clickable Specifies whether view is clickable or not
5 android:id Set id of the view. Note – Id must be uniques within an xml file



Example of Android Radio Group Widget

At first, we will create an android application. Then, we will use this widget in the 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 RadioGroup. 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 group in the application.

2. Modify Values folder

No values folder have been modified. So, we are not going to mention them here.

3. Use Radio group 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>

In activity_main.xml file, we have used two radioButtons in a radioGroup. Since radioGroup provides mutually exclusive selection. Either radioButton with id radioMale or radioButton with id radioFemale will be selected at a time.

Now, we will access radioGroup in java file to perform some actions on it.

4. Access Radio Group Widget in java file

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

package com.tutorialwing.radiogroup;

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.java file, we have accessed radioGroup. Then, we have set a listener, checkedChangeListener, to show a toast message, that displays currently selected radioButton, whenever selection changes in the radioGroup.

Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside 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.radiogroup"
		  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 we run the application, we will get output as shown above.

That’s the end of tutorial on Android Radio Group widget.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at tutorialwing@gmail.com. We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners