Question

I am experimenting with a TabHost widget. I did not realise that the TabActivity has been deprecated. I am having trouble actually displaying my TabHost on the screen when I run my activity. At present My XML file contains the following:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

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

And my MainActivity.java contains:

public class MainActivity extends Activity {

TabHost myTabBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

How can I get the TabHost to appear on the screen? Every time I google TabHost all of the tutorials involve extending TabActivity!

Thanks.

EDIT: Am I on the right track when doing something like this?

myTabBar = (TabHost)findViewById(android.R.id.tabhost);
    myTabBar.setup();
Was it helpful?

Solution

Activities in tabs was deprecated and Google agrees that it's a bad idea to do this, you have to consider using views as the contents of your Tabs, here an exemple from CommonsWare book you can find helpful resources.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top