tatiana_c tatiana_c - 8 days ago 8
Android Question

Can I create dynamically RadioGroup with RadioButtons inside it (w/o xml)?

I want to create RadioGroup, with RadioButton list inside it, in the onCreate function. I want to do it as exercise w/o using xml-layout. Is it possible? Thanks.

Answer

Something like this:

....
RadioGroup group = new RadioGroup(this); 
group.setOrientation(RadioGroup.HORIZONTAL);
RadioButton btn1 = new RadioButton(this);
btn1.setText("BTN1");
group.addView(btn1);
RadioButton btn2 = new RadioButton(this);
group.addView(btn2);
btn2.setText("BTN2");
.... 
RadioButton btnN = new RadioButton(this);
group.addView(btnN);
btnN.setText("BTNN");
yourLayout.addView(group);
....