1

I'm tryihng to set up a tabs layout in android. Inside the layout with id 'tab1', is there any way that I can reference another xml file? I just don't want to have a huge messy single file. Instead, I would like to reference a different file in each of the layouts with ids 'tab1', 'tab2', and 'tab3'.

Here is tabs layout:

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

<TabHost
    android:id="@+id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
</TabHost>


</LinearLayout>
Jad
  • 115
  • 2
  • 5
  • 10

2 Answers2

8

You can add the other xml file using include tag of android as below shown example :

suppose this below layout is in xml named layout1.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello, I am a TextView" />
    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button" />
</LinearLayout>

Then we can add the above xml in another layout xml, which may have other widgets too, as shown below:

<include layout="@layout/layout1" />

Here is a reference to android developement site

Android Killer
  • 18,174
  • 13
  • 67
  • 90
3

Have a look at the < include /> tag. Android include tag

Entreco
  • 12,738
  • 8
  • 75
  • 95
  • 1
    You should at least include some xml showing the usage of the tag. If Google ever changes site and breaks the link, your answer will not provide much info. – psubsee2003 Jan 21 '13 at 14:24