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 what is the exact problem
– Nilesh Rathod
Sep 1 at 5:48


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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)