Search code examples
javaandroidhashmapradio-buttonandroid-radiogroup

Generate dynamic RadioGroup programatically and setOnCheckedChangeListener on it


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


Solution

  • Refer this..

    May be it will help you for getting radiobutton dynamically