Pages Navigation Menu

Coding is much easier than you think

How To Create Radio button in an Android Application using Eclipse:

How To Create Radio button in an Android Application using Eclipse:

In this tutorial, you will be Learning, How to display Radio button in an activity?

Here you go!!!!


Before creating an android application using Eclipse, let us see some of the key terms involved while developing this application.

Key terms involved in this project:

Intent: Android uses a special class called Intent to move from screen to screen. Intent describe what an application wants done. The two most important parts of the intent data structure are the action and the data to act upon. Typical values for action are MAIN (the front door of the application), VIEW, PICK, EDIT, etc. The data is expressed as a Uniform Resource Indicator (URI). For example, to view a website in the browser, you would create Intent with the VIEW action and the data set to a Website-URI.

Radiobuttons: Radio buttons allow the user to select one option from a set. You should use radio buttons for optional sets that are mutually exclusive if you think that the user needs to see all available options side-by-side. When the user selects one of the radio buttons, the corresponding RadioButton object receives an on-click event. To define the click event handler for a button, add the android:onClick attribute to the <RadioButton> element in your XML layout.

To define the click event handler for a button, add the android:onClick attribute to the <RadioButton> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

Menu: Interface for managing the items in a menu. By default, every Activity supports an options menu of actions or options. You can add items to this menu and handle clicks on your additions. The easiest way of adding menu items is inflating an XML file into the Menu via MenuInflater. The easiest way of attaching code to clicks is via onOptionsItemSelected(MenuItem) and onContextItemSelected(MenuItem). Different menu types support different features:

Context menus: Do not support item shortcuts and item icons.

Options menus: The icon menus do not support item check marks and only show the item’s condensed title. The expanded menus (only available if six or more menu items are visible, reached via the ‘More’ item in the icon menu) do not show item icons, and item check marks are discouraged.

Sub menus: Do not support item icons, or nested sub menus.

The tutorial is about how to display radio button in the UI of the android application.

This project is developed in Eclipse 4.2 (Juno) and tested with Android 2.2

If you are new to new to android application project, to create a new project in Eclipse refer Creation of Android Project.


Now let’s go to the coding section. This project requires following files.

Source Code:

  1. – Activity (main screen)

Activity Files:

  1. activity_radiobutton.xml – main screen/ layout

res – Values:

  1. strings.xml – strings available in layout/activity

Manifest file:

  1. AndroidManifest.xml

Here are the coding for the above files.

package com.example.radiobutton; //Radiobutton package
import android.os.Bundle; // A mapping from String values to various Parcelable types.
import; // Required to create an activity.
import android.view.Menu; // Interface for managing the items in a menu.
public class Radiobutton extends Activity { // All classes must extend activity
	protected void onCreate(Bundle savedInstanceState) { // Create a new activity
		setContentView(R.layout.activity_radiobutton); // display activity radiobutton when app starts 
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(, menu);
		return true;


<!-Setting the linear layout with vertical orientation-->
<LinearLayout xmlns:android=""

<!-- Displays text on the screen -->

android:text="Which is your favourite operating system???"

<!--Group of radios available in the activity -->

<!--Radiobutton1 -->

<RadioButton android:id="@+id/windows"
android:text="Windows" />

<!--Radiobutton2 -->

<RadioButton android:id="@+id/linux"
android:text="Linux" />

<!--Radiobutton3 -->

<RadioButton android:id="@+id/mac"
android:text="Mac" />

Menu : activity_radiobutton.xml

<menu xmlns:android="" >
<!--Menu settings-->



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    android:versionName="1.0" >

        android:targetSdkVersion="8" />

        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />



<?xml version="1.0" encoding="utf-8"?>

<!--Application name -->
    <string name="app_name">Radiobutton</string>
<! -- Text message -->

    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>

Styles. xml:

<!-- specify properties such as height, padding, font color, font size, background color -->

        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    <style name="AppBaseTheme" parent="android:Theme.Light">
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->


Run the android application:

Android applications can be run on the android devices. You can either connect a hardware to the system to test the application or you can use the android virtual devices (AVD) Manager to create/manage the virtual devices running on emulator to run the application.

If you are new to android application project, to create a new project in Eclipse refer Creation of Android Project.

To run the application/ project, Build your project using Project -> Build Project.

Build project

This will show errors in Console window at the bottom of the working area in case your project contains.

If your build is successful, Run your application using Run -> Run or Press Ctrl +F11.

Run Project

Upon running the application, the emulator will be launched with the selected/ available AVD created on your screen.

Start Emulator

To test your application, unlock the screen and double click on your app.

You can see your app with the image set during the android project creation in AVD.

AVD Home screen

Now the activity created for radiobutton is shown as in the figure below.

Radiobutton widget


Click on the radiobutton to choose an option as shown below.


Thus the android application project is executed successfully.


Click Here To Share This..!!