Let me know if you have any issues or questions.
This works with, and probably requires, a AVD version of 2.1 or thereabouts.
contents of droidTest1.java:
package androidforbeginners.droidTest1;
import android.app.Activity;
import android.os.Bundle;
public class droidTest1 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
contents of main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="combining layouts"
/>
<include android:id="@+id/cell1" layout="@layout/layout2" />
<include android:id="@+id/cell2" layout="@layout/layout3" />
</LinearLayout>
Contents of layout2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="100px"
android:background="#0033cc"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="40px"
android:text="layout2"
/>
<CheckBox
android:layout_width="fill_parent"
android:layout_height="40px"
/>
</LinearLayout>
Contents of layout3.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="100px"
android:background="#0066cc"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="layout3"
/>
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Output:
You could also include multiple occurrences of the one layout in your main.xml like this if you wanted:
contents of main.xml (revised):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="combining layouts"
/>
<include android:id="@+id/cell1" layout="@layout/layout2" />
<include android:id="@+id/cell2" layout="@layout/layout2" />
<include android:id="@+id/cell3" layout="@layout/layout2" />
<include android:id="@+id/cell4" layout="@layout/layout2" />
</LinearLayout>
Although if you do this, I can't see a way to reference individual repeating items.
I think include is more including a single layout across multiple Activities.
Let me know in the comments if you know a way.
Till next time: Enjoy!
Hi there, I don't know if you've found already a solution to reference the individual items that are repeating through the use of . I just did and want to share it here because it can be useful for people who want to use this also.
ReplyDeleteIn your code you can get the view for example cell2 with this piece of code:
View vcell2 = (View)this.findViewById(R.id.cell2);
Afterwards you can get an item in cell2 view with the following piece of code:
Button btn = (Button)vcell2.findViewById(R.id.btn);
I hope this helps more of you guys! It helped me a lot.
Thanks for this post, had some trouble when trying to use the include tag but works fine now.
ReplyDeleteactually in my app am having a layout containing 7 text views and i want that wenever i click on a particular link a new layout will be called. but wen i tried to do this by using setContentview from the mainactivity it didn't show any expected result.
ReplyDeletehow can i call all the 7 layouts from a single activity?