Skip to main content

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

Popular posts from this blog

How to use NumberPicker in Android

Table of contentsNumberPicker ExamplesetOnValueChangedListeneronValueChange()setWrapSelectorWheel()setMinValue()setMinValue()setMaxValue()NumberPicker with String ArraysetDisplayedValues()String Array1. NumberPicker Example activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context=".MainActivity" android:background="#ffffff" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25dp" android:text="Select a number..." /> <NumberPicker android:id="@+id/np" …

TextView new line (multiline) in android

TextView new line (multiline) TextView display text on android app. by default, TextView show text on a single line, and if it is long then TextView take more lines to display its text. android developers can generate a new line on TextView both programmatically and syntactically. android developers can make a multiline TextView without splitting text to multiline by android:minLines attribute.

the following android example code demonstrate us how can we syntactically create a new line on TextView widget by xml layout file and string resource file.

the simplest way to create a new line on TextView is android:text attribute. android:text attribute allow us to display text on android app. we can add a simple '\n' to TextView text where we want to start a new line. in this way we can create a multiline TextView widget in android app. we can assign android:text attribute value by this way android:text="Line1 \n Line2 \n Line3" for a TextView widget. this value will…

How to change TextView font size in android

TextView font size TextView widget display text on android application. we can set or change TextView font size statically by declarative syntax in xml layout file or programmatically at run time in java file. even we can use an xml file source to define font size.

the following example code demonstrate us how can we define TextView font size in xml layout file and how can we uses dimens.xml to reference font size. in this example we did not changes any coding in java file, so here we only include the layout xml file and dimens.xml file. activity_main.xml <LinearLayout 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:orientation="vertical" android:layout_margin="25dp" tools:context=".MainActivity" > <TextView android:id=&…