Android RadioButton Example


In this tutorial you will learn how to create a RadioButton, which allows user to choose one of several options. Putting multiple RadioButton elements in one container makes it impossible to choose more than one option, which means that when user toggles one button it automatically cancels the previous one. There is only two things that you will need to do to have a RadioButton element in your application: 1. Add RadioButton element to xml layout file and 2. Add onClick() event handler to your activity class.


1. Adding Android RadioButton element


         To add this element to your application you need to open xml layout file (which is located in the res\layout directory) and add the following code to it:

activity_main.xml file

 <RadioGroup 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:paddingBottom="@dimen/activity_vertical_margin"  
   android:paddingLeft="@dimen/activity_horizontal_margin"  
   android:paddingRight="@dimen/activity_horizontal_margin"  
   android:paddingTop="@dimen/activity_vertical_margin"  
   android:background="#9000"  
   tools:context=".MainActivity" >  
   <TextView  
     android:id="@+id/text1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:textSize="20sp"  
     android:textColor="#FFFFFF"  
     android:text="@string/hint" />  
   <RadioButton   
    android:id="@+id/button1"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text="@string/yellow"  
    android:textColor="#FFD700"/>  
   <RadioButton   
    android:id="@+id/button2"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text="@string/red"  
    android:textColor="#FF6347"/>  
   <RadioButton   
    android:id="@+id/button3"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text="@string/blue"  
    android:textColor="#4169E1"/>  
   <TextView  
     android:id="@+id/text2"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:textSize="20sp"  
     android:textColor="#ffffff"  
     android:text="@string/empty" />   
 </RadioGroup>  


          As you have probably noticed I am using five string resources in this file. To add them go to res\values directory and add the following lines to your strings.xml file:

 <string name="yellow">Yellow</string>  
 <string name="red">Red</string>  
 <string name="blue">Blue</string>  
 <string name="hint">Select one of the colors:</string>  
 <string name="empty"> </string>  

2. Adding onClick() event handler

           Now to find out which option has been chosen we need to add onClick() event handler to each of our RadioButtons. To do that we need to modify activity class:

MainActivity.java file 

 package radiobuttonexample.tuts.com;  
 import android.os.Bundle;  
 import android.app.Activity;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.widget.RadioButton;  
 import android.widget.TextView;  
 public class MainActivity extends Activity {  
      private RadioButton first_button;  
      private RadioButton second_button;  
      private RadioButton third_button;  
      private TextView text;  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
           first_button = (RadioButton) findViewById(R.id.button1);  
           second_button = (RadioButton) findViewById(R.id.button2);  
           third_button = (RadioButton) findViewById(R.id.button3);  
           text = (TextView) findViewById(R.id.text2);  
           onClickMethod();  
      }  
      private void onClickMethod() {  
           // TODO Auto-generated method stub  
           OnClickListener radio_button_click = (new OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                     RadioButton rb = (RadioButton)v ;   
                     text.setText ("You've selected " + rb.getText());                           
                }  
           });  
           first_button.setOnClickListener(radio_button_click);  
           second_button.setOnClickListener(radio_button_click);  
           third_button.setOnClickListener(radio_button_click);  
      }  
 }  

DEMO

Android RadioButton Example
Android RadioButton Example 
Source code