0

I was trying to add a View multiple times by using a for loop, but I was getting an error

Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

My XML file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_display_list"
    android:id="@+id/root_layout">
</LinearLayout>

My class file

ViewGroup ll = (ViewGroup) findViewById(R.id.root_layout);
TextView tv = (TextView) new TextView(this);
tv.setText("helloworld");
tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));

//adding views in loop
for(int i=0;i<=5;i++)
{
    ll.addView(tv);
}

Where am I going wrong?

3
  • 2
    You're trying to add the same TextView five times. Move everything except the findViewById() line to inside your loop. And you probably don't want MATCH_PARENT params for both dimensions. Commented Jun 24, 2016 at 7:34
  • Why is it so? why to create the object 5 times when we can create it once and use it 5 times? Commented Jun 24, 2016 at 7:39
  • That doesn't really make any sense. If you want five TextViews, you need to create five of them. Commented Jun 24, 2016 at 7:40

4 Answers 4

3

You cannot add the same View multiple times to the same layout.

The following should work:

ViewGroup ll = (ViewGroup) findViewById(R.id.root_layout);

for(int i=0; i<=5; i++)
{
    TextView tv = (TextView) new TextView(this);
    tv.setText("helloworld");
    tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
    ll.addView(tv);
}

Also, setting either the height or the width (maybe both, depending on the orientation of your LinearLayout) of the TextView to WRAP_CONTENT might make more sense.

Sign up to request clarification or add additional context in comments.

Comments

0

You should use LinearLayout, and need to create textview and params everytime, because you can't add single view multiple times -

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

          //adding views in loop
          for(int i=0;i<=5;i++)
          {
             TextView tv = (TextView) new TextView(this);
             tv.setText("helloworld");
             ll.addView(tv, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
          }

Hope it will help :)

Comments

0

Try this:

for(int i=0;i<=5;i++){
    ViewGroup ll = (ViewGroup) findViewById(R.id.root_layout);
    TextView tv = (TextView) new TextView(this);
    tv.setText("helloworld");
    tv.setLayoutParams(new        LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));

    ll.addView(tv);

}

1 Comment

What are you asking here? Please explain.
-1

Try it, may it help..

 //adding views in loop
    for(int i=0;i<=5;i++)
    {
    if(tv.getParent()!=null)
    {
        ((ViewGroup)tv.getParent()).removeView(tv);
    }
    ll.addView(tv); 
}

thanks..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.