1

I Made below Code for Displaying Dynamic Radiogroup and radiobuttons. But I cannot understand how to get checked button into setOnCheckedChangeListener() method. Some time multiple RadioButton will click on same RadioGroup. I don't know how?

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll);

menuSize = 4;

for(i = 0; i < menuSize; i++)
{
    int menuId = controller.getMenuid(i));
    int subMenuSize = controller.getSubMenu(menuId).size();  // Dynamic value from MVC architechture

    TextView textViewHeading = new TextView(getApplicationContext()); // RadioGroup Heading
    textViewHeading.setText(controller.getMenuName(i));    // Set RadioGroup Heading

    linearLayout.addView(textViewHeading);

    RadioGroup radioGroup = new RadioGroup(getApplicationContext());
    radioGroup.setId(i);

    for(j = 0; j < subMenuSize; j++)
    {
        RadioButton radioButton = new RadioButton(getApplicationContext());

        radioButton.setId(j);

        // Get value from HashMap<Integer, ArrayList<SubMenuClass>>
        // Value is used for RadioButton
        radioButton.setText( controller.getSubMenu(menuId).get(j).getSubMenuName()); 

        radioGroup.addView(radioButton);
    }

    radioGroup.setOnCheckedChangeListener(TryRadioButtons.this);

    linearLayout.addView(radioGroup);
}

I want to check on ButtonSubmit Click that every RadioGroup have one RadioButton must checked. So how can I get that RadioButton in ButtonSubmit ClickEvent?

Thnks in Advance

Bhavesh Butani
  • 281
  • 2
  • 18
  • You have to start with the linear layout like here on the first line and then loop through its children. – greenapps Mar 30 '15 at 13:18
  • I want to get radiobuttons in setOnCheckedChangeListener() method – Bhavesh Butani Mar 30 '15 at 13:45
  • Yes i know. I knew that already. You dont have to say that again. What did yoy do with my suggestion? – greenapps Mar 30 '15 at 14:08
  • I solved it. In my code there is problem is in setId. I set ID the value of "i" and "j" that will may b same for sometime. so it will check multiple radiobuttons in same radiogroup. by d way thank you for your reply. – Bhavesh Butani Mar 31 '15 at 05:16

2 Answers2

1

do this way,

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);
Jignesh Jain
  • 1,518
  • 12
  • 28
0

Refer this..

May be it will help you for getting radiobutton dynamically

Community
  • 1
  • 1
Pranav P
  • 1,755
  • 19
  • 39