java - 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" />

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

All Answers
  • Translate

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

  • Translate

    This should work,

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

  • Translate

    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(;

    Then when you need to get the selected radio option.

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

  • Translate

    try this

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

  • Translate

    You can either use OnCheckedChangeListener or can use getCheckedRadioButtonId()

  • Translate

    You can use:

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

  • Translate

    //use to get the id of selected item

    int selectedID = myRadioGroup.getCheckedRadioButtonId();

    //get the view of the selected item

    View selectedView = (View)findViewById( selectedID);

  • Translate

    It worked perfectly for me in this way:

        RadioGroup radioGroup = (RadioGroup) findViewById(;
        int radioButtonID = radioGroup.getCheckedRadioButtonId();
        RadioButton radioButton = (RadioButton) radioGroup.findViewById(radioButtonID);
        String selectedtext = (String) radioButton.getText();

  • Translate

    All you need is to set values first to your RadioButton, for example:

    RadioButton radioButton = (RadioButton)findViewById(;      
    radioButton.setId(1);        //some int value

    and then whenever this spacific radioButton will be chosen you can pull its value by the Id you gave it with

    RadioGroup radioGroup = (RadioGroup)findViewById(;                     
    int whichIndex = radioGroup.getCheckedRadioButtonId(); //of course the radioButton
                                                           //should be inside the "radioGroup"
                                                           //in the XML


  • Translate
      btnDisplay.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
            int selectedId=radioSexGroup.getCheckedRadioButtonId();

  • Translate
     int index_selected = radio_grp.indexOfChild(radio_grp

  • Translate

    you can do


    from the radio group .

    Here it is sample :


  • Translate

    just use this:

        int index = 2;
        boolean option3Checked = radioGroup.getCheckedRadioButtonId() == radioGroup.getChildAt(2).getId();

  • Translate

    You can simply

    -declare the radio group and a radio button out of onCreate method

    private RadioGroup GrupName;
    private RadioButton NameButton;

    -set the view id in the onCreate method

    GrupName = (RadioGroup) findViewById(;

    -take the radiobutton id selected using

    int id = GroupName.getCheckedRadioButtonId();

    -use the id to match the buttonselected view

    NameButton = (RadioButton) findViewById(id);

    -finally get the value of the radio button

    String valueExpected = NameButton.getText().toString();


    int valueExpectedIn = Integer.parseInt(valueExpected);