Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TabHost not showing in the screen

I'm trying to use TabHost on my app, and I simply dragged it to my activity using the design, but when I run it, it just won't appear, just get the white screen, does anyone knows why?

<TabHost
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tabHost"
    android:layout_gravity="center_horizontal">

    <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>

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

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"></LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical"></LinearLayout>

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

like image 287
Bruno Braga Avatar asked Jan 16 '15 05:01

Bruno Braga


People also ask

How to use TabHost in Android?

Important Methods of TabHostaddTab(TabSpec tabSpec): This method is used to add the new tab onto a tab widget. Whenever a new tab is being specified using TabSpec class, one needs to add that tab in our TabHost. // initiate TabHost TabHost tabhost = findViewById(R. id.

What is TabHost in Android?

In Android, TabHost is a Container for tabbed window view. This object holds two children one is set of tab labels that the user clicks to select a specific tab and other is a FrameLayout object that displays the content of that page.

Which are the two child of the tab host?

This object holds two children: a set of tab labels that the user clicks to select a specific tab, and a FrameLayout object that displays the contents of that page.


1 Answers

This is happening simply because you just can't create TabHost using only XML code . You need to add TabSpecs to the TabHost like this:

TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);

TabSpec tab1 = tabHost.newTabSpec("First Tab");
TabSpec tab2 = tabHost.newTabSpec("Second Tab");
TabSpec tab3 = tabHost.newTabSpec("Third Tab");

tab1.setIndicator("Tab1");
tab1.setContent(new Intent(this,TabActivity1.class));

tab2.setIndicator("Tab2");
tab2.setContent(new Intent(this,TabActivity2.class));

tab3.setIndicator("Tab3");
tab3.setContent(new Intent(this,TabActivity3.class));

tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
like image 144
SMR Avatar answered Sep 23 '22 18:09

SMR