Search code examples
javaandroidradio-button

Add RadioGroup in dynamic LinearLayout


I dynamically generate radiobuttons for an app (ex: 3 radiobuttons) in a LinearLayout I call parentLinearLayout. Problem is, they don't go automatically in a group, so if I check a button and then another, both check instead of un-checking the first one. So, I thought I should do a RadioGroup.

The app itself is a questionnaire app, and every question has a different number of possible answers. I create from a radiobutton layout the number of answers as radiobuttons.

Problem is, I get an error saying that I should remove the old view before doing so, but if I do this I lose my generated linearlayout: The specified child already has a parent. You must call removeView() on the child's parent first.

Here is my code:

public void drawRAnswers(int pst){
    int drawables = qmclist.get(pst).getAnswers().size();
    RadioGroup l1 = new RadioGroup(this);
    for (int i=0;i<drawables;i++){
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View rowView = inflater.inflate(R.layout.radiobutton, null);
        // Add the new row before the add field button.
        parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount());
        rowView.setId(i);
        RadioButton rd = (RadioButton) rowView.findViewById(R.id.radiobtn);
        l1.addView(rd); <== HERE IS THE PROBLEM
        rd.setText(current.getAnswers().get(i).getAns());
    }
}

Any ideas how I could fix this? Thanks


Solution

    1. You are creating a new layout during every iteration in loop.
    2. By inflating a new view, you are losing the old views
    3. Every time you are using the same id R.id.radiobtn for to find a view and add the same view again in the layout which is not possible hence the issue.

    for (int i=0;i<drawables;i++){
            // 1,2
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           // 
           final View rowView = inflater.inflate(R.layout.radiobutton, null);
    
            parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount());
            rowView.setId(i);
            RadioButton rd = (RadioButton) rowView.findViewById(R.id.radiobtn);
            l1.addView(rd); // same view with same id R.id.radiobtn
            rd.setText(current.getAnswers().get(i).getAns());
        }
    

    Solution :

    public void drawRAnswers(int pst){
        int drawables = qmclist.get(pst).getAnswers().size();
        // create a radio group as container with direction
        RadioGroup l1 = new RadioGroup(this);
        l1.setOrientation(LinearLayout.VERTICAL);
        for (int i=0;i<drawables;i++){
            // create radio button, with id and text
            RadioButton rd = new RadioButton(this);
            rd.setId(i);
            rd.setText(current.getAnswers().get(i).getAns());
            // add radio button into group
            l1.addView(rd);
        }
        // finally, add radio group with buttons into parent layout  
        parentLinearLayout.addView(l1, parentLinearLayout.getChildCount());
    }