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

[post_middile_section_ad]

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/MainActivity.java 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.

[post_middile_section_ad]

package com.example.saira_000.myapplication;
import android.support.v7.app.ActionBarActivity;
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)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      addListenerRadioButton();
   }

      private void addListenerRadioButton()
      {
      rg1 = (RadioGroup) findViewById(R.id.radioGroup);
      b1 = (Button) findViewById(R.id.button2);
      b1.setOnClickListener(new View.OnClickListener()
      {

         @Override
         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=”http://schemas.android.com/apk/res/android”
   xmlns:tools=”http://schemas.android.com/tools”
   android:layout_width=”match_parent”
   android:layout_height=”match_parent”
   android:paddingBottom=”@dimen/activity_vertical_margin”
   android:paddingLeft=”@dimen/activity_horizontal_margin”
   android:paddingRight=”@dimen/activity_horizontal_margin”
   android:paddingTop=”@dimen/activity_vertical_margin”
   tools:context=”.MainActivity”>   

   <TextView
      android:id=”@+id/textView1″
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:text=”Example of Radio Button”
      android:layout_alignParentTop=”true”
      android:layout_centerHorizontal=”true”
      android:textSize=”30dp” />      

   <TextView
      android:id=”@+id/textView2″
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:text=”W3School”
      android:textColor=”#ff87ff09″
      android:textSize=”30dp”
      android:layout_above=”@+id/imageButton”
      android:layout_centerHorizontal=”true”
      android:layout_marginBottom=”40dp” />      

   <ImageButton
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:id=”@+id/imageButton”
      android:src=”@drawable/abc”
      android:layout_centerVertical=”true”
      android:layout_centerHorizontal=”true” />      

   <Button
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:id=”@+id/button2″
      android:text=”ClickMe”
      android:layout_alignParentBottom=”true”
      android:layout_centerHorizontal=”true” />   

   <RadioGroup
      android:id=”@+id/radioGroup”
      android:layout_width=”fill_parent”
      android:layout_height=”fill_parent”
      android:layout_below=”@+id/imageButton”
      android:layout_alignLeft=”@+id/textView2″
      android:layout_alignStart=”@+id/textView2″>         

      <RadioButton
         android:layout_width=”142dp”
         android:layout_height=”wrap_content”
         android:text=”JAVA”
         android:id=”@+id/radioButton”
         android:textSize=”25dp”
         android:textColor=”@android:color/holo_red_light”
         android:checked=”false”
         android:layout_gravity=”center_horizontal” />         

      <RadioButton
         android:layout_width=”wrap_content”
         android:layout_height=”wrap_content”
         android:text=”ANDROID”
         android:id=”@+id/radioButton2″
         android:layout_gravity=”center_horizontal”
         android:checked=”false”
         android:textColor=”@android:color/holo_red_dark”
         android:textSize=”25dp” />         

      <RadioButton
         android:layout_width=”136dp”
         android:layout_height=”wrap_content”
         android:text=”HTML”
         android:id=”@+id/radioButton3″
         android:layout_gravity=”center_horizontal”
         android:checked=”false”
         android:textSize=”25dp”
         android:textColor=”@android:color/holo_red_dark” />         
   </RadioGroup>      
</RelativeLayout>

[post_middile_section_ad]

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″?>
<resources>
   <string name=”app_name”>My Application</string>
</resources>

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

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
   package=”com.example.saira_000.myapplication” >   
   <application
      android:allowBackup=”true”
      android:icon=”@drawable/ic_launcher”
      android:label=”@string/app_name”
      android:theme=”@style/AppTheme” >      

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

[post_middile_section_ad]

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.