Android RadioGroup Control


A set of radio buttons combine to make use of the RadioGroup class. The working of a RadioGroup is linked with the working of the each one of radio buttons involved in it. So, if a user checks one of the radio buttons belonging to a Radio Group then it will automatically uncheck any of the previously checked options from the set.

Also Read: Android CheckBox Control

Various RadioGroup Attributes
A few of the major attributes of RadioGroup are listed below. For the complete list of RadioGroup attributes, you can visit the official Android documentation. If you will take a look at this official doc, you will also get to know about the methods that can be used to change the values of the RadioGroup attributes at the runtime.

S. No. Attribute Description
1 android:checkedButton * The value of this attribute is accepted as the id of the child radio button.
* Make sure that you keep the state of this radio button as checked so that this radio group can be enabled.

The below-provided list of RadioGroup attributes is inherited from the android.view.View class.

S. No. Attribute Description
1 android:id * This attribute’s value is considered as the view’s identifier name.
2 android:background * The value of this attribute accepts a drawable value to be kept as the background for the view.
3 android:visibility * The visibility of the view is controlled by this attribute.
4 android:contentDescription * The briefly described content of the view is passed in this attribute.
5 android:onClick * This is the method which gets invoked whenever the view is clicked.

For the better understanding of the use of RadioGroup UI control, take a look at the following example. In here, you will get a step-to-step guide to create an Android application with the help of the Linear layout and RadioGroup control.

Step 1: Open the Android Studio IDE and create an Android application under the name of myapplication. Keep it in the com.example.w3school.myapplication package. This step has already been mentioned in the Hello World Example chapter.

Step 2: The file already contains the definitions of the fundamental lifecycle callback methods. So, you just need to add a click event to this file. To locate this file, look into the src/ directory.

 package com.example.w3school.myapplication;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast; 

public class MainActivity extends Activity
   private RadioGroup radioSexGroup;
   private RadioButton radioSexButton;
   private Button btnDisplay;   
   protected void onCreate(Bundle savedInstanceState)
      btnDisplay.setOnClickListener(new View.OnClickListener()
         public void onClick(View v)
            int selectedId=radioSexGroup.getCheckedRadioButtonId();
            radioSexButton=(RadioButton)findViewById(selectedId);            Toast.makeText(MainActivity.this,radioSexButton.getText(),Toast.LENGTH_SHORT).show();

Step 3: There is another file which needs a section of code to be included in it. Look under the res/layout/ path and you will find the activity_main.xml file. In here, add all the required Android UI controls. 

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=””

      android:text=”Radio button”
      android:textSize=”35dp” />   

      android:textColor=”@android:color/holo_green_dark” />   

      android:layout_alignEnd=”@+id/textView” />   


         android:textSize=”25dp” /> 

         android:layout_weight=”0.13″ />

      android:text=”    Are you?”
      android:layout_alignStart=”@+id/imageView” />   

      android:text=”New Button”
      android:layout_centerHorizontal=”true” />

Step 4: Let the content of the res/values/strings.xml file remain as it is. The required changes will automatically be done by the Android Studio IDE.

<?xml version=”1.0″ encoding=”utf-8″?>
   <string name=”app_name”>My Applicaiton</string>
   <string name=”example_radiogroup”>Example showing RadioGroup</string>

Step 5: Keep the AndroidManifest.xml file untouched. It does not require any kind of modifications.

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=””
   package=”com.example.w3school.myapplication” >   
      android:theme=”@style/AppTheme” >      

         android:name=”com.example.My Application.MainActivity”
         android:label=”@string/app_name” >      
            <action android:name=”android.intent.action.MAIN” />
            <category android:name=”android.intent.category.LAUNCHER” />

Step 6: The last step is to verify all the changes done and check whether the application runs successfully or not. For this, you need to have an already created AVD. We are assuming you are done with this process during the environment setup of the Android Studio. Now, open your project’s activity file and click on the Run button. The Studio then start installing your myapplication app on the AVD and if everything is fine then you will be displayed with the final output on the Emulator window.

The final output screen will display two Radio Buttons, which are Male and Female. A user needs to select either of the two buttons. Once a user selects one of the Radio Buttons and hit on the New Button button control then he/she will be displayed with a toast message.