Radio Button in Android Example


Radio buttons are used to to let the users select only ONE of given choices. Choices are arranged in group of two or more.This can be used as an alternative to Spinners. Only one choice will visible in spinner in default state, whereas all the choices will be visible in radio button.

Step 1:
Create a <RadioGroup> in the layout xml file.
This is used to create a set of radio buttons out of which the user will be able to select only one at a time. When a radio button is selected others will automatically be de-selected.

Step 2:
Add two or more <RadioButton> elements.

Step 3:
Add controls to the radio button. This can be done in two ways, one is using android:onClick property of the <RadioButton> element.
Example:

 <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="26dp"
            android:layout_marginTop="36dp"
            android:onClick="callbackMethod"
            android:text="Choice 1" />

We should have 'callbackMethod(View view) in our activity class to handle the click event.

public void callbackMethod(final View view){
     Toast.makeText(this,((RadioButton) view).getText(), Toast.LENGTH_SHORT).show();
    }

Another way of adding control is using a button and its onclick listener. Something like common form 'submit' button.

button.setOnClickListener(new OnClickListener() {
//Get the radio button id from radio group
//Get the content of radio button
}


Sample application:
Layout xml file:

<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"
    tools:context=".MainActivity" >

    <RadioGroup 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="26dp"
            android:layout_marginTop="36dp"
            android:onClick="callbackMethod"
            android:text="Choice 1" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="26dp"
            android:layout_marginTop="36dp"
            android:onClick="callbackMethod"
            android:text="Choice 2" />
        
        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="26dp"
            android:layout_marginTop="36dp"
            android:onClick="callbackMethod"
            android:text="Choice 3" />
    </RadioGroup>

</RelativeLayout>


Activity class:

package com.example.radiobutton;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
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.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public void callbackMethod(final View view){
     Toast.makeText(this,((RadioButton) view).getText(), Toast.LENGTH_SHORT).show();
    }
}


Hope this Radio button example in android helps..

Screenshot:


Source code of this application:
RadioButton.zip






Reactions:

0 comments :

Post a Comment