Android Radio Buttons Tutorial

Radio buttons are one of the common UI elements in android apps. Radio buttons allow the user to select an option from a set. There are two key classes related to radio buttons- android.widget.RadioButton and android.widget.RadioGroup.

RadioButton : The RadioButton has two states: either checked or unchecked. Once a RadioButton is checked, it cannot be unchecked.

RadioGroup : A RadioGroup is used to group together one or more RadioButton views, thereby allowing only one RadioButton to be checked within the RadioGroup.

Lets consider an example of two radio buttons grouped in a radio group. When button is clicked, display which radio button is selected.

1. Create a new project by going to File ⇒ New Android Project and fill required details:
  
    Project Name : RadioButtonExample
    Package : com.sunil.radiobutton
    Main Activity : MainActivity.java
    Layout : main.xml
    Build Target : Android 2.2 

2. Open the res/layout/main.xml file and insert the following code:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <RadioButton
        android:id="@+id/radio_hindi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onRadioButtonClicked"
        android:text="Hindi" />

    <RadioButton
        android:id="@+id/radio_english"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onRadioButtonClicked"
        android:text="English" />

</RadioGroup>

As you can see we simply add two radio buttons using <RadioButton> element and because both the radio buttons are mutually exclusive we simply group them together inside a <RadioGroup> element. We also define the click event handler for both the radio buttons using android:onClick attribute to the <RadioButton> element. In our case the value for android:onClick attribute is onRadioButtonClicked is the name of the method that we want to call in response to a click event. We must implement this method in the Activity that hosting the layout.


3. Now open the MainActivity.java file and add the following code:
package com.sunil.radiobutton;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }


    // Event handler for radio buttons
    public void onRadioButtonClicked(View view) {

        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch(view.getId()) {
        case R.id.radio_hindi:
            if (checked)
                Toast.makeText(this, "You've selected: Hindi", Toast.LENGTH_LONG).show();
            break;
        case R.id.radio_english:
            if (checked)
                Toast.makeText(this, "You've selected: English", Toast.LENGTH_LONG).show();
            break;
        }
    }

}


4. Now run your project. (See the screenshots below)

Android Radio Buttons

When you run your project you will see that no button is selected by default. When you click on "Hindi" button, you will be notified by a Toast message.

How to make a Radio button selected by default ?
To make a radio button is selected by default, simply put android:checked="true" within the <RadioButton> element.

That's it ! You can also download the source code of this tutorial. 

Download Code






Be the first to comment

Post a Comment

Recent Posts

Site Stats

Our Apps

Get it on Google Play

©2012-2014 AndroidAspect - All rights reserved | Powered by Blogger

Creative Commons License