How to get the selected index of a RadioGroup in 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.

Answers


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 selected RadioButton Text on RadioGroup

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

This should work,

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

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 = findViewById(R.id.radio_group);

Then when you need to get the selected radio option.

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

You can either use OnCheckedChangeListener or can use getCheckedRadioButtonId()


You can use:

RadioButton rb = (RadioButton) findViewById(rg.getCheckedRadioButtonId());

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);
            }
        });

//use to get the id of selected item

int selectedID = myRadioGroup.getCheckedRadioButtonId();

//get the view of the selected item

View selectedView = (View)findViewById( selectedID);

 int index_selected = radio_grp.indexOfChild(radio_grp
                .findViewById(radio_grp.getCheckedRadioButtonId()));

you can do

findViewById

from the radio group .

Here it is sample :

((RadioButton)my_radio_group.findViewById(R.id.radiobtn_veg)).setChecked(true);

Need Your Help

Create Object in Splash Screen to be used later

android sharedpreferences listactivity extends implements

I read from a RSSfeed and create an object of that feed. As this takes time I want to have it read the feed during the spash screen. After the splash screen loads the Main Menu appears which extends

JSF cascading drop down

jsf-2

i am experiencing a weird behavior. i have cascading requirement in my project so i did a small code snippet in my personal laptop which worked but the same thing did not work in my project explain...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.