Search code examples
androidxmltabsandroid-tabhosttabwidget

Android tabHost is invisable? Why won't it show with this XML?


In my XML editor on Eclipse I see the tabhost, and it looks exactly how I want it, but when I run it I don't see the tabHost at all, how can I make sure it's visable?

This is my XML. Again, the editor shows it, but when I run it it doesn't, the activity which inflates it doesn't do much yet, just change the two textfields. Any ideas?

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

    <TextView
        android:id="@+id/name"
        android:layout_width="238dp"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:layout_gravity="top|left"
        android:layout_marginTop="7dp"
        android:layout_marginLeft="3dp"
        android:layout_row="0"
        android:gravity="top"
        android:maxLines="1"
        android:padding="3dp"
        android:text="Motherfucking Acer Laptop X314134"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <View
        android:layout_height="2dp"
        android:layout_width="fill_parent"
        android:layout_row="1"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:background="#FF000000"
        />

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:layout_gravity="center_horizontal"
        android:layout_row="2" >

        <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/Info"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </LinearLayout>

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

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

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

    <TextView
        android:id="@+id/score"
        android:layout_column="1"
        android:layout_marginRight="3dp"
        android:layout_marginTop="4dp"
        android:layout_gravity="right"
        android:layout_row="0"
        android:gravity="center_vertical"
        android:padding="3dp"
        android:text="100%"
        android:textSize="24sp" />

    <Space
        android:id="@+id/space2"
        android:layout_width="1dp"
        android:layout_height="40dp"
        android:layout_column="0"
        android:layout_gravity="left"
        android:layout_row="0" />

    <Space
        android:id="@+id/space1"
        android:layout_width="1dp"
        android:layout_height="415dp"
        android:layout_column="0"
        android:layout_gravity="left"
        android:layout_row="2" />

</GridLayout>

Solution

  • You have to do this programmatically, for example:

     tabhost = (TabHost) findViewById(android.R.id.tabhost);
        tabhost.setup();
        TabSpec ts = tabhost.newTabSpec("tag1"); 
        ts.setContent(R.id.tab1);
        ts.setIndicator("First Tab");
        tabhost.addTab(ts);
    
        ts = tabhost.newTabSpec("tag2"); 
        ts.setContent(R.id.tab2);
        ts.setIndicator("Second Tab");  
        tabhost.addTab(ts);
        ts= tabhost.newTabSpec("tag3");
        ts.setContent(R.id.tab3);
        ts.setIndicator("Third Tab");
        tabhost.addTab(ts);