Thursday, January 8, 2015

How to use Radio button click event in android

Radio button click event
The following android example code demonstrate us how can we use Radio Button onClick event (click event) in an android app. Radio buttons allow the app users to select one option (RadioButton) from a set (RadioGroup). A single RadioButton element in our XML layout create a single Radio button (option). Radio buttons are mutually exclusive, so we must group them together inside a RadioGroup object.

RadioGroup object grouping multiple Radio buttons elements those are located inside it. RadioGroup object ensures that only one Radio button can be selected at a time.

RadioGroup Class extends LinearLayout. RadioGroup is a subclass of LinearLayout that has a vertical orientation by default. We need to import android.widget.RadioGroup and android.widget.RadioButton to manage programmatically both RadioGroup and RadioButton object. Checking one Radio button from a Radio group unchecks any previously checked Radio button within the same Radio group.

When an app user selects one of the Radio button, the corresponding RadioButton object receives an onClick event. We can define a click event handler for a Radio button in xml layout by adding the android:onClick attribute to the RadioButton element. We set the same onClick method name for all RadioButton elements inside a Radio group.

The activity which host the Radio buttons handles the click event. The android:onClick attribute defined method must have a signature as shown the MainActivity.java file. The method must be public, return a void and define a View as its only parameter (it is the View that was clicked).

We should use a Switch Statement to determine which RadioButton element is clicked. In the Switch Statement, we check every RadioButton elements checked status to determine it is currently checked or not. After getting the current checked Radio button element we can take more action depending on checked item's content.
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RadioGroup
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="left"
    android:layout_margin="25dp"
    android:background="#FF0"
    android:paddingRight="50dp"
    >

    <RadioButton
        android:id="@+id/Radio_Windows_Phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Windows Phone 7 Silverlight Cookbook"
        android:onClick="onRadioButtonClicked"
        />
    <RadioButton
        android:id="@+id/Radio_IOS"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Swift Development with Cocoa"
        android:onClick="onRadioButtonClicked"
        />
    <RadioButton
        android:id="@+id/Radio_Android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android SQLite Essentials"
        android:onClick="onRadioButtonClicked"
        />

</RadioGroup>
MainActivity.java

package com.cfsuman.me.myapplication5;

import android.graphics.Typeface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioButton;


public class MainActivity extends ActionBarActivity {

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

    public void onRadioButtonClicked(View v)
    {
        //require to import the RadioButton class
        RadioButton rb1 = (RadioButton) findViewById(R.id.Radio_Windows_Phone);
        RadioButton rb2 = (RadioButton) findViewById(R.id.Radio_IOS);
        RadioButton rb3 = (RadioButton) findViewById(R.id.Radio_Android);

        //is the current radio button now checked?
        boolean  checked = ((RadioButton) v).isChecked();

        //now check which radio button is selected
        //android switch statement
        switch(v.getId()){

            case R.id.Radio_Windows_Phone:
                if(checked)
                    //if windows phone programming book is selected
                    //set the checked radio button's text style bold italic
                    rb1.setTypeface(null, Typeface.BOLD_ITALIC);
                    //set the other two radio buttons text style to default
                    rb2.setTypeface(null, Typeface.NORMAL);
                    // reqire to import Typeface class
                    rb3.setTypeface(null, Typeface.NORMAL);
                break;

            case R.id.Radio_IOS:
                if(checked)
                    //if ios programming book is selected
                    //set the checked radio button's text style bold italic
                    rb2.setTypeface(null, Typeface.BOLD_ITALIC);
                    //set the other two radio buttons text style to default
                    rb1.setTypeface(null, Typeface.NORMAL);
                    rb3.setTypeface(null, Typeface.NORMAL);
                break;

            case R.id.Radio_Android:
                if(checked)
                    //if android programming book is selected
                    //set the checked radio button's text style bold italic
                    rb3.setTypeface(null, Typeface.BOLD_ITALIC);
                    //set the other two radio buttons text style to default
                    rb1.setTypeface(null, Typeface.NORMAL);
                    rb2.setTypeface(null, Typeface.NORMAL);
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
The following images display the output of this example code. When someone select/check/click an item from Radio group, the specified RadioButton element generate a click event. In the java file we handle the specified RadioButton element's click event. In this example, we convert the selected/checked/clicked Radio button's content text to bold and italic format.

More android examples