Android using layouts as a template for creating multiple layout instances
Asked Answered
C

2

26

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.

Charlenacharlene answered 27/10, 2011 at 13:27 Comment(0)
E
44

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>
Elocution answered 27/10, 2011 at 14:2 Comment(4)
Thanks a lot! That's sorted it... I think I need to get my head around the different inflate(....) calls.Charlenacharlene
Is there a way to simply copy the layout during the loop instead of using inflator.inflate() each iteration? It seems to me that inflating is very expensive compared to simply copying.Elsa
yaa, that;s it. ThanxPrefrontal
Any way to usse this in fragment? getActivity().setContentView(parent messes up the whole thing but I see the result. Also I seem to get this error alot after implementing this android.content.res.Resources$NotFoundException: String resource ID #0x7f0e00a2Labiche
S
6

To annyone still looking for a similar solution, apparently you can also use include directly in xml and still be able to refer to them in code:

LinearLayout row1 = (LinearLayout) findViewById(R.id.row1)
TextView text1 = row1.findViewById(R.id.text);

LinearLayout row2 = (LinearLayout) findViewById(R.id.row2)
TextView text2 = row2.findViewById(R.id.text);

Source: Romain Guy

Sexagesima answered 16/2, 2014 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.