1

as part of my application I have to create four radioButtons within a radiogroup and get the radio button clicked. I wrote the following code but my selectId attribute gives values 2131034181 when first option is selected and 2131034182 when second option is selected and so on, there's change in unit digit of value when 3 and 4 are clicked. Why is it so?

int selectId=rbg.getCheckedRadioButtonId();
RadioButton selected= (RadioButton) rbg.findViewById(selectId);

String selected_user = (String) selected.getText();
Harika
  • 23
  • 2
  • 8
  • 1
    I think you are looking for index of selected radio button? If yes, check this: http://stackoverflow.com/a/6441097/1911652 – Atul May 01 '16 at 10:56
  • the id which you get by `rbg.getCheckedRadioButtonId()` gives you the id, w.r.t. the radio group, of the selected radio button. This id is not w.r.t. the layout. The id of the radio button is different w.r.t the radio group and the layout. The id obtained above may differ by only the units digit or by more than one digit. This behaviour doesn't has any general trend. – aQwus jargon May 23 '19 at 19:23

1 Answers1

0

To get radiobutton,use findViewById from context instead of radiogroup,So change

RadioButton selected= (RadioButton) rbg.findViewById(selectId);
String selected_user = (String) selected.getText();

to

  RadioButton selected = (RadioButton)findViewById(selectId);
  String selected_user = selected.getText().toString();
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
  • My problem is with the return value of getCheckedRadioButtonId(), the code is working fine but returns integer values 2131034181 instead of 1. – Harika Nov 04 '14 at 15:01
  • @user3747888 Do you want to get radio button index or id of radio button? – Giru Bhai Nov 04 '14 at 17:03