Joyson
Joyson

Reputation: 1653

How to add a inflated view multiple times to a linear Layout?

I am trying to add a inflated view inside a LinearLayout container. However i am getting the child already has a parent issue. Following is my xml file having the container multiple times. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@android:color/black"
        ></LinearLayout>
</LinearLayout>

item_button.xml is the xml that i want to inflate into a container.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:background="@color/purple"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:text="Button 1"
        android:padding="20dp"
        android:background="@color/colorAccent"
        />

</LinearLayout>

Following is the java code inside onCreate method. I want to add the childView multiple times to the container.:

        View childView;
        LayoutInflater inflater = (LayoutInflater)   this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        childView = inflater.inflate(R.layout.item_button, null);
        container.addView(childView);
        container.addView(childView);

However adding the child mulitple times to the view gives following error:

The specified child already has a parent. You must call removeView()
on the child's parent first.

Upvotes: 10

Views: 9243

Answers (2)

Muhammed Refaat
Muhammed Refaat

Reputation: 9103

This happens because you are adding the same view multiple times, to achieve what you want to achieve you have to only add the view once, and hence you have to create a new view each time you want to add a view to the layout.

let's suppose you want to add it two times:

View childView1, childView2;
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
childView1 = inflater.inflate(R.layout.item_button, null);
childView2 = inflater.inflate(R.layout.item_button, null);
container.addView(childView1);
container.addView(childView2);

OR, if you want to add the view many more times:

View v1, v2, v3;
View[] childViews = new View[]{v1,v2,v3};
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for(int v = 0; v < childViews.length; v++){
    childViews[v] = inflater.inflate(R.layout.item_button, null);
    container.addView(childViews[v]);
}

Upvotes: 9

Rehan
Rehan

Reputation: 3285

As the inflated view has already been added to the parent layout, you need to inflate it every time before adding it. Your code should be something like this:

View childView;
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < count; i++) {
    childView = inflater.inflate(R.layout.item_button, null);
    container.addView(childView);
}

where count obviously is the number of times you want to add the item_button layout.

Upvotes: 7

Related Questions