Android RadioButton Control - RadioButton Control Tutorial | W3school

A Radio Button is an Android UI control which permits a user to select one of the options from a set. This UI control is generally used in applications where a developer includes MCQs (Multiple Choice Questions). Well, a RadioButton can go through in either of the two available states. These two states of a RadioButton are: checked and unchecked.

Also Read: Android Button Control


The above figure shows you a set of three Radio Buttons: Yes, Maybe, and No. Also, you can easily see that the user has selected Yes out of the available three options.

The following example will help you to understand the use of RadioButton control in your application. You will get a step-to-step guide for the creation of an application where you will be using Linear layout and the RadioButton control itself.

Step 1: Open Android Studio IDE. In here, create a new application with the name myapplication. Make sure you store it under the com.example.w3school.myapplication package. You can even go to the Hello World Example chapter for the better understanding of this step.

Step 2: Open src/ file. Here, you need to include the below provided section of code where you are required to add a click event. The already existing content of the file carries the definitions of the fundamental lifecycle callback methods.


package com.example.saira_000.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast; 

public class MainActivity extends ActionBarActivity
   RadioGroup rg1;
   RadioButton rb1;
   Button b1;
   protected void onCreate(Bundle savedInstanceState)

      private void addListenerRadioButton()
      rg1 = (RadioGroup) findViewById(;
      b1 = (Button) findViewById(;
      b1.setOnClickListener(new View.OnClickListener()

         public void onClick(View v)
            int selected=rg1.getCheckedRadioButtonId();
            rb1=(RadioButton)findViewById(selected);            Toast.makeText(MainActivity.this,rb1.getText(),Toast.LENGTH_LONG).show();

Step 3: Open res/layout/activity_main.xml file. Here, add the code to include required Android UI control.

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

      android:text=”Example of Radio Button”
      android:textSize=”30dp” />      

      android:layout_marginBottom=”40dp” />      

      android:layout_centerHorizontal=”true” />      

      android:layout_centerHorizontal=”true” />   


         android:layout_gravity=”center_horizontal” />         

         android:textSize=”25dp” />         

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


Step 4: Remember that you don’t need to define these new constants in the strings.xml file. The Studio IDE will handle this on its own. Well, the content of the strings.xml will look like the following. And if you want to see the content of this file then follow the res/values/ path. 

<?xml version=”1.0″ encoding=”utf-8″?>
   <string name=”app_name”>My Application</string>

Step 5: The default content of the AndroidManifest.xml file is given below. 

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=””
   package=”com.example.saira_000.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 final step is to run this newly created myapplication application. To do so, you need to make sure that you have already created your AVD during the environment setup. Open one of the activity files of your project. Now, click on the Run button from the toolbar. After this, the Android Studio will initiate the installation of your app on the AVD. If the compilation process completes successfully then the output will be shown on the Emulator window.

On the final output screen, if the user selects any Radio Button then the user will get the same name printed on the screen as a toast message. So, for an instance, if a user clicks on ANDROID Radio Button then he/she will get a message displaying ANDROID on the screen.