Beginner
    AJAX
    Android
    ASP.Net
    C#
    Cloud Computing
    Crystal Report
    Database
    Drupal
    HTML5
    JavaScript
    Joomla
    JQuery
    MVC
    php
    Reporting
    SharePoint
    SQL Server
    VB.Net
    Windows Phone
    WordPress
    WPF
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> Android >> Radio Buttons control in Android




Radio Buttons control in Android - MindStick

Radio Buttons control in Android

In this article, I am going to explain how to create two mutually-exclusive (in a group) radio buttons (enabling one disables the other), using the RadioGroup and RadioButton widgets. When either radio button is pressed, a toast message will be displayed.

         Start a new project, named RadioButtonDemo.

         Open the res/layout/main.xml file and add two RadioButtons, nested in a RadioGroup (inside the LinearLayout):

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

 

   <RadioGroup android:layout_width="fill_parent"

                android:layout_height="wrap_content"

                android:orientation="vertical" >

              <RadioButton android:id="@+id/radio_male"

                            android:layout_width="wrap_content"

                            android:layout_height="wrap_content"

                            android:text="Male"/>

              <RadioButton android:id="@+id/radio_female"

                            android:layout_width="wrap_content"

                            android:layout_height="wrap_content"

                            android:text="Female"/>

   </RadioGroup>

</LinearLayout>

 

         Now when each RadioButton is selected, you need a View.OnClickListener. so add the following code in the Activity:

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.RadioButton;

import android.widget.Toast;

 

public class RadioButtonActivity extends Activity {

      

       private OnClickListener radioListener= new OnClickListener() {

              public void onClick(View v) {

                     RadioButton rb=(RadioButton)v;

                     Toast.makeText(RadioButtonActivity.this, rb.getText(),
                                                          Toast.
LENGTH_SHORT).show();

              }

       };

      

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        final RadioButton radioMale=(RadioButton)findViewById(R.id.radio_male);

        final RadioButton radioFemale=(RadioButton)findViewById(R.id.radio_female);

        radioMale.setOnClickListener(radioListener);

        radioFemale.setOnClickListener(radioListener);

    }

}

         Run the application
Your output is something like below:

Radio Buttons control in Android

When any radio button is selected by the user, the radiobutton text will display in the toast message.



 Author Information
Name: Rohit Kesharwani
Member Since: 8/8/2011
Article Submitted Date: 10/20/2011
Location:     India
Report Abuse Form
Reason:    
 
Total Online Users: 2858
Advertisement
mindstick
Advertise with Us
  
Dudelabs
Copyright © 2009 - 2013MindStick. All Rights Reserved.