Getting Selected Radio Button Value from Radio Group in Android  

I have been doing some light Android development and was trying to figure out how to get the selected value of a radio button from a given radio group. I spent some time on Google trying to find a tutorial or some help but had little success. What follows is a tutorial of how I ended up accomplishing the task.

1
2
3
4
5
6
7
8
9
10
11
12
// Gets a reference to our radio group
// rBtnDigits is the name of our radio group (code not shown)
RadioGroup g = (RadioGroup) findViewById(R.id.rBtnDigits); 
 
// Returns an integer which represents the selected radio button's ID
int selected = g.getCheckedRadioButtonId();
 
// Gets a reference to our "selected" radio button
RadioButton b = (RadioButton) findViewById(selected);
 
// Now you can get the text or whatever you want from the "selected" radio button
b.getText();

The comments should explain the code sufficiently, but I’ll provide a brief overview:

Basically, you’re getting a reference to your radio group. Once you have that reference, you ask your radio group what button is selected and it returns to you the ID of the selected radio button. With the ID you can then get a reference to your specific radio button by using the findViewById method again.

Once you have a reference to your selected radio button you can do whatever you would normally do with it. Of course, this code should be placed wherever you need this functionality. For instance, if you wanted to find out what radio button the user has selected when you click a “submit” button, then you would want to place the code in the OnClickListener function for the “submit” button.

[Update 2/2/2012]

A much better way I discovered of finding out which radio button they selected is to set up if/else logic to check each of the buttons to see if it is selected. Placing each radio button into a group will ensure that only one is selected. Here is some code to illustrate this concept.

1
2
3
// As class variables - define your buttons
private RadioButton button1 = null;
private RadioButton button2 = null;
1
2
3
// In your onCreate() method get a reference to the buttons
button1 = (RadioButton) findViewById(R.id.rbtnButton1);
button2 = (RadioButton) findViewById(R.id.rbtnButton2);
1
2
3
4
5
6
// Wherever needed - check to see which button is selected ("checked")
if(button1.isChecked()) {
  // Perform whatever action/logic you need
} else if(button2.isChecked()) {
  // Perform whatever action/logic you need
}

The code below was submitted by Michael (link to comment) and is more efficient to use if you have a set option group (i.e. non-dynamically created buttons which are placed at design time). Please consider the following:

1
2
3
4
5
6
7
8
9
10
11
12
RadioGroup g = (RadioGroup) findViewById(R.id.rBtnDigits);
 
switch (g.getCheckedRadioButtonId())
{
case R.id.rbtnButton1
//do something
break;
 
case R.id.rbtnButton2
//do something
break;
}

Anyway, there you have it. Hopefully you found this helpful. Let me know in the comments below if you have any questions or feedback.

Comments

7 Responses to “Getting Selected Radio Button Value from Radio Group in Android”

  1. Pkmmte Xeleon says:

    Thank you so much!
    This works perfectly and is also simple to understand.


  2. Neeraj Rautela says:

    Thank you so much this works fine in case where use dynamica radio button and radio group


  3. Meena says:

    How to select gender like male or female dynamically when i click submit ?


    • Matt Moses says:

      Applying what I explained above, to get the gender when the button is clicked your activity could support the following functionality:

      * This assumes you have a layout that supports the GUI requests. My layout is simply a radio group containing two radio buttons, a submit button, and a text view to display the results.

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      
      package moses.matt.gendercheck;
       
      import android.app.Activity;
      import android.os.Bundle;
      import android.view.View;
      import android.view.View.OnClickListener;
      import android.widget.Button;
      import android.widget.RadioButton;
      import android.widget.RadioGroup;
      import android.widget.TextView;
       
      public class GendercheckActivity extends Activity {
       
          private RadioButton femaleBtn = null;
          private RadioButton maleBtn = null;
          private TextView result = null;
          private Button submit = null;
       
          /** Called when the activity is first created. */
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
       
              // In your onCreate() method get a reference to the buttons
              femaleBtn = (RadioButton) findViewById(R.id.gender_female);
              maleBtn = (RadioButton) findViewById(R.id.gender_male);
       
              // Get a reference to the text view
              result = (TextView) findViewById(R.id.result);
       
              // Get a reference to the submit button
              submit = (Button) findViewById(R.id.submit);
              // Hook up OnClickListener
              submit.setOnClickListener(submitOcl);
          }
       
          public OnClickListener submitOcl = new OnClickListener() {
       
      		public void onClick(View arg0) {
      	           // Wherever needed - check to see which button is selected ("checked")
      	           if(femaleBtn.isChecked()) {
      	        	result.setText("You choose female.");
      	           } else if(maleBtn.isChecked()) {
      	        	result.setText("You choose male.");
      	           }			
      		}
       
          };
      }
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      
      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical" >
       
          <RadioGroup
              android:id="@+id/radioGroup1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" >
       
       
              <RadioButton
                  android:id="@+id/gender_female"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:checked="true"
                  android:text="@string/gender_female" />
       
       
              <RadioButton
                  android:id="@+id/gender_male"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/gender_male" />
       
          </RadioGroup>
       
       
          <Button
              android:id="@+id/submit"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/submit" />
       
       
          <TextView
              android:id="@+id/result"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/youchoose"
              android:textAppearance="?android:attr/textAppearanceMedium" />
       
      </LinearLayout>

  4. Michael says:

    Thank you for the post. I got what I needed from it. Although your examples can be simplified greatly if you have a set option group (i.e. not adding options dynamically in code but rather setup at design-time). This is especially true of your second example (since it is more “hard coded”) See below.

    RadioGroup g = (RadioGroup) findViewById(R.id.rBtnDigits);

    switch (g.getCheckedRadioButtonId())
    {
    case R.id.rbtnButton1
    //do something
    break;

    case R.id.rbtnButton2
    //do something
    break;
    }

    The advantage of this approach is it only performs the findViewById call once, which is relatively expensive.


Post a Comment...

This entry was posted on Wednesday, October 12th, 2011 at 7:44 pm and is filed under Updates.

Tags: , ,