0

I have tried with five ways to create dynamic RadioButton in one RadioGroup , but always i have problem. I have read 20 tutorials but nothing...When i have commented RadioGroup, everything works , but i can select all radio buttons...

This is my code:

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_login, container, false);

            //List of Radio Buttons
            final List<RadioButton> radioButtons=new ArrayList<RadioButton>();

            //RadioGroup
            RadioGroup radioGroup = new RadioGroup(getActivity());

            //LinearLayout 
            final LinearLayout linearLayout_chooseAccount=(LinearLayout)rootView.findViewById(R.id.chooseAccount_linearlayout);

            List views = new ArrayList();



            for(int i = 0;i < usersEmails.size();i++){

                View view = inflater.inflate(R.layout.fragment_chooseaccount_item,null);
                LinearLayout linearLayout = (LinearLayout)view.findViewById(R.id.chooseaccount_click);

                RadioButton radioButton = new RadioButton(getActivity());
                radioButtons.add(radioButton);
                radioGroup.addView(radioButtons.get(i));


                linearLayout.addView(radioGroup);


                TextView text = (TextView)view.findViewById(R.id.username2);
                text.setText(usersEmails.get(i)+"");

                views.add(view);
            }
           for(int i = 0; i < views.size(); i++)
           linearLayout_chooseAccount.addView((View) views.get(i));


   return rootView;

}

This is fragment_chooseaccount_item.xml:

   <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:maxWidth="120dp"
         android:textAppearance="?android:attr/textAppearanceLarge"
         android:text="User name"
         android:id="@+id/username2"
         android:layout_marginRight="10dp"
         android:layout_gravity="center_vertical"
         android:textSize="12sp"/>

This is xml:

<ScrollView
     android:layout_width="fill_parent"
     android:layout_height="170dp">
     <LinearLayout
        android:id="@+id/chooseAccount_linearlayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center"></LinearLayout>

</ScrollView>

This is error:

: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
MilanNz
  • 1,323
  • 3
  • 12
  • 29
  • Possible duplicate of [How to add radio button dynamically as per the given number of counts?](https://stackoverflow.com/questions/19380526/how-to-add-radio-button-dynamically-as-per-the-given-number-of-counts) – Mehdi Dehghani Jul 25 '18 at 09:04

1 Answers1

0

It looks like your code is trying to add the same view multiple times:

List views = new ArrayList();

for(int i = 0;i < usersEmails.size();i++){

    View view = inflater.inflate(R.layout.fragment_chooseaccount_item,null);

    views.add(view);

}

for(int i = 0; i < views.size(); i++)
       linearLayout_chooseAccount.addView((View) views.get(i)); //This will fail
foobar
  • 695
  • 9
  • 17