Here's a working example of including one layout inside another.
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.
Wednesday, January 5, 2011
Including layouts: a working example
By Magesh Kumar
Posted at 1:57 AM
Android
No comments
I think include is more including a single layout across multiple Activities.
About the Author
Nulla sagittis convallis arcu. Sed sed nunc. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.
View all posts by: BT9
- Draw 9-patch Tutorial
- Cracking apk to get java code
- Handling Button clicks in a ListView Row
- Android Menu and Submenu Example
- Larry Page: Android powers Google Glass
- Android GPS How to calculate the distance between two GPS coordinates?
- SlidingDrawer
- Gradient dividers in Android
- Thread Concepts:
- How to Crack a Software
Popular Posts
Total Pageviews
Powered by Blogger.
Contributors
About Me
Trending Stories
- Android (72)
- Blackberry (1)
- CSS (6)
- How to Crack a Software (1)
- HTML (14)
- Java (1)
- Java Scripts (6)
- Photoshop (11)
Blog Archive
-
▼
2011
-
▼
January
- How to populate a Spinner widget from an Array
- Spinner down arrow stretching?
- Want to display a quick message to your users? Use...
- How to create Status Bar Notifications
- Including layouts: a working example
- Using EditText's inputType to control what type of...
- Handling Button clicks in a ListView Row
-
▼
January
0 comments: