Android using layouts as a template for creating multiple layout instances

gunboatmedia picture gunboatmedia · Oct 27, 2011 · Viewed 23.4k times · Source

OK, So I understand how to use the include tag but I've run into a problem.

Basically I want to have a layout defined in xml which has a couple of TextViews and an ImageView in it. I then want to iterate across an array and populate fields within the xml layout depending on whats in an array(which is populated on runtime). Thus making multiple copies of the xml layout and populating the fields with unique data. Now i've got no idea how you can re-use this LinearLayout in this way as the TextViews and ImageViews within it have a constant id and I need to make multiple copies of this layout.

Is there any way to inflate a resource and then make a copy of it, that would work... So

LinearLayout one = new LinearLayout(inflater.inflate(R.layout.home, container, false));

^ There is no constructor like that unfortunately.

The only other way is to do it all programatically but I would of preferred to have the properties of the views and the LinearLayout in xml rather than in the code. It's like I want the LinearLayout to be a template which you can make copies of I guess... Really not sure if that's possible.

Answer

Craigy picture Craigy · Oct 27, 2011

You can easily do this, you just have to break it down. First you load the layout that you want to insert your dynamic views into. Then you inflate your subview and populate it as many times as you need. Then you add the view to your parent layout, and finally set the content view of the activity to the parent view.

Here's an example:

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout parent = (LinearLayout) inflater.inflate(R.layout.main, null);

for (int i = 0; i < 3; i++) {
    View custom = inflater.inflate(R.layout.custom, null);
    TextView tv = (TextView) custom.findViewById(R.id.text);
    tv.setText("Custom View " + i);
    parent.addView(custom);
}

setContentView(parent);

here is the main.xml file that I am inserting into:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

</LinearLayout>

and here is the custom.xml view that I inflate, populate and dynamically insert:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>