Search code examples
androidxmlandroid-tabhost

Use same xml layout file for TabHost


I have a tab host and I want to use the same layout file for all three tabs. But when I try to do this I only see the layout on one of the tabs not all three.

myTabHost =(TabHost) findViewById(R.id.TabHost01);
    myTabHost.setup();

    TabHost.TabSpec spec1 = myTabHost.newTabSpec("First Tab");
    spec1.setIndicator("First Tab", getResources().getDrawable(android.R.drawable.ic_menu_add));
    spec1.setContent(R.id.tab1);
    myTabHost.addTab(spec1);

myTabHost.addTab(myTabHost.newTabSpec("Tab Two").
            setIndicator("Tab Two", getResources().getDrawable(android.R.drawable.ic_menu_edit)).setContent(R.id.tab1));

And then in my tab hosts xml activity I have

 <TabHost
    android:id="@+id/TabHost01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            </TabWidget>
        <!-- container of tabs -->
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <include
                android:id="@+id/tab1"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                layout="@layout/item_list_view">

But on Tab Two I don't see the item_list_view?

How can I use the same xml for both tabs?

Thanks for the help


Solution

  • myTabHost =(TabHost) findViewById(R.id.TabHost01);
    
    myTabHost.setup();
    
    TabHost.TabSpec spec1 = myTabHost.newTabSpec("First Tab");
    spec1.setIndicator("First Tab", getResources().getDrawable(android.R.drawable.ic_menu_add));
    spec1.setContent(R.id.tab1);
    myTabHost.addTab(spec1);
    

    You are making it porogramatically right use Tabhpost content factory for the content