How to get the selected index of a RadioGroup in Android

radio group android
android radio group set selected
radio button selection in android
radiogroup get checked radio button android
get selected radio button java android
how to get radio button id from radiogroup in android
how to get selected radio button value from listview in android
radio button onclick android

Is there an easy way to get the selected index of a RadioGroup in Android or do I have to use OnCheckedChangeListener to listen for changes and have something that holds the last index selected?

example xml:

<RadioGroup android:id="@+id/group1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
    <RadioButton android:id="@+id/radio1" android:text="option 1" android:layout_width="wrap_content" android:layout_height="wrap_content" />
    <RadioButton android:id="@+id/radio2" android:text="option 2" android:layout_width="wrap_content" android:layout_height="wrap_content" />
    <RadioButton android:id="@+id/radio3" android:text="option 3" android:layout_width="wrap_content" android:layout_height="wrap_content" />
    <RadioButton android:id="@+id/radio4" android:text="option 4" android:layout_width="wrap_content" android:layout_height="wrap_content" />
    <RadioButton android:id="@+id/radio5" android:text="option 5" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</RadioGroup>

if a user selects option 3 I want to get the index, 2.

You should be able to do something like this:

int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton);

If the RadioGroup contains other Views (like a TextView) then the indexOfChild() method will return wrong index.

To get the selected RadioButton text on the RadioGroup:

 RadioButton r = (RadioButton) radioButtonGroup.getChildAt(idx);
 String selectedtext = r.getText().toString();

You should be able to do something like this: int radioButtonID = radioButtonGroup.getCheckedRadioButtonId(); View radioButton = radioButtonGroup. You could have a reference to the radio group and use getCheckedRadioButtonId () to get the checked radio button id. Take a look here. RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radio_group); Then when you need to get the selected radio option.

This should work,

int index = myRadioGroup.indexOfChild(findViewById(myRadioGroup.getCheckedRadioButtonId()));

This example demonstrates how do I get selected index of the radio group in android.Step 1 − Create a new project in Android Studio, go to File  AndroidApps/Applications Mobile Development. This example demonstrates how do I get selected index of the radio group in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

You could have a reference to the radio group and use getCheckedRadioButtonId () to get the checked radio button id. Take a look here

RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radio_group);

Then when you need to get the selected radio option.

int checkedRadioButtonId = radioGroup.getCheckedRadioButtonId();
if (checkedRadioButtonId == -1) {
    // No item selected
}
else{
    if (checkedRadioButtonId == R.id.radio_button1) {
        // Do something with the button
    }
}

Android code snippets for android app developer. Saturday, August 8, 2015. How to get selected item from a RadioGroup in Android. activity_main.xml. In this android programming source code example, we are going to get the selected index of a RadioGroup in Android. You can copy and adopt this source code example to your android project without reinventing the wheel. Below is a step by step source code to get the selected index of a RadioGroup in Android.

try this

        RadioGroup  group= (RadioGroup) getView().findViewById(R.id.radioGroup);
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                View radioButton = radioGroup.findViewById(i);
                int index = radioGroup.indexOfChild(radioButton);
            }
        });

selected index of a RadioGroup in Android. activity_main.xml. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  To get the selected RadioButton text on the RadioGroup: RadioButton r = (RadioButton) radioButtonGroup.getChildAt (idx); String selectedtext = r.getText ().toString (); Please log in or register to add a comment.

You can either use OnCheckedChangeListener or can use getCheckedRadioButtonId()

Is there an easy way to get the selected index of a RadioGroup in Android or do I have to use OnCheckedChangeListener to listen for chan Question said "set selected INDEX", here's how to set it by index: ((RadioButton)radioGroup.getChildAt(index)).setChecked(true); .. Additional info: It seems that Google wants you to use id instead of index, because using id is more bug proof. For example, if you have another UI element in your RadioGroup, or if another developer

In this android programming source code example, we are going to set selected index of a RadioGroup in Android. You can copy and adopt this source code  You can use above code inside your onCreate method or any other method. In this first i find RadioGroup Id and after when user click on button we find Selected Radio button from group and save its id in a variable and find the selected radio button value. 🙂

Is there an easy way to get the selected index of a RadioGroup in Android or do I have to use OnCheckedChangeListener to listen for changes  Following is the example of defining multiple RadioButton controls in RadioGroup, one TextView control and one Button control in RelativeLayout to get the selected values of RadioButton controls when we click on Button in the android application. Create a new android application using android studio and give names as RadioButtonExample.

The selection is identified by the unique id of the radio button as defined in the XML layout file. XML Attributes. See RadioGroup Attributes  How can I get the selected radiobutton from a radiogroup in android? This site uses cookies for analytics, personalized content and ads. By continuing to browse this site, you agree to this use.

Comments
  • But what if those buttons do not have their android:id attributes set?
  • @BP i have same doubt in accessing radio buttons when none of the parent or the radio buttons id is set.
  • @neuront As long as you do radioGroup.findViewById(radioButtonID) it'll work. RadioGroup does set 1, 2, 3, 4, and so on as view's IDs, so if you do search for them within it's context, it'll work
  • This not working if default (and untouched by User) RadioButton is setted.
  • @NinjaCoding If you made the same mistake as me, you have to set the default (initial) radio button by calling radioGroup.check(selectedRadioButton.id), not radioButton.setChecked(true).
  • Use getActivity().findViewById() if you are using in the Fragment.
  • yes, this is the id of the checked radio button, but what about the index of the radio button in the radio group?
  • i can get the id, i want the index, they are different.
  • what do you mean by index? is it in a list view?
  • if i have 5 radiobuttons in the radiogroup, and the user selects the 3rd one i want to get 2, the index of the selected radiobutton in the radiogroup. this is not a listview.
  • just to clarify if the user selects the 3rd button you want to get the 2nd one? radio groups and buttons do not have indexes.