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.
"Inflating" a view means taking the layout XML and parsing it to create the view and viewgroup objects from the elements and their attributes specified within, and then adding the hierarchy of those views and viewgroups to the parent ViewGroup.
To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”. Let us create an Android application with LinearLayout containing two Button widgets as children. We shall center align these children using gravity attribute.
To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .
The LayoutInflater class is used to instantiate the contents of layout XML files into their corresponding View objects. In other words, it takes an XML file as input and builds the View objects from it.
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]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With