Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    APIs
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
Radio Buttons control in Android


by Rohit Kesharwani on 10/20/2011 7:01:38 PM

Views: 1966       Comments: 0

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.

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Rohit KesharwaniRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 2702
Advertisement
MindStick Cleaner
Advertise with Us
  
Copyright © 2013MindStick. All Rights Reserved.