I want to add radiobutton programatically and set first item being selected?
I want to add radiobutton programatically and set first item being selected?
Here is my code which I created bunch of radio buttons from my Json.
for (int i = 0; i < level_array_list.size(); i++)
RadioButton radioButton = new RadioButton(getActivity());
radioButton.setText(level_array_list.get(i).getLevelName());
radioButton.setId(Integer.parseInt(level_array_list.get(i).getLevel_id()));
radioGrp.addView(radioButton);
Please help. Thanks in advance!
i am stucked here
i want to be selected first item which is loaded from json array ? i cant able to do that
– Shameem Ahsan
Sep 1 at 5:53
2 Answers
2
i want to be selected first item which is loaded from json array ?
You need to use radioButton.setChecked(true);
inside loop when i = 0
(means first radio button)
radioButton.setChecked(true);
i = 0
Try this
for (int i = 0; i < level_array_list.size(); i++)
RadioButton radioButton = new RadioButton(getActivity());
radioButton.setText(level_array_list.get(i).getLevelName());
radioButton.setId(Integer.parseInt(level_array_list.get(i).getLevel_id()));
if(i==0)
radioButton.setChecked(true);
radioGrp.addView(radioButton);
Try this code. This will checked the first item. Hope it helps!
ArrayList<RadioButton> radioButtonsList = new ArrayList<>();
for (int i = 0; i < level_array_list.size(); i++)
RadioButton radioButton = new RadioButton(getActivity());
radioButton.setText(level_array_list.get(i).getLevelName());
radioButton.setId(Integer.parseInt(level_array_list.get(i).getLevel_id()));
radioButtonsList.add(radioButton);
radioGrp.addView(radioButton);
RadioButton radioButton = radioButtonsList.get(0);
radioButton.setChecked(true);
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
i am stucked here
what is the exact problem– Nilesh Rathod
Sep 1 at 5:48