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 TextView
s 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 TextView
s and ImageView
s 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.
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>
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
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