Wednesday 3 April 2013

How to add tab layout without letting the activity to extend TabActivity?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TabHost tabHost = (TabHost) findViewById(R.id.mytabhost);
    tabHost.setup();

    TabSpec tab1 = tabHost.newTabSpec("TAB_1");
    tab1.setIndicator("Tab 1");
    tab1.setContent(R.id.tab1);
    tabHost.addTab(tab1);

    //tab 2 etc...
    TabSpec tab2 = tabHost.newTabSpec("TAB_2");
    tab2.setIndicator("Tab 2");
    tab2.setContent(R.id.tab2);
    tabHost.addTab(tab2);
}
-----------------------------------------------------------------------------------------------------------------------
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mytabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


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

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

        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_height="fill_parent" android:layout_width="fill_parent">
            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:id="@+id/tab1">
                <!-- tab 1 content goes here -->
            </LinearLayout>
            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:id="@+id/tab2">
                <!-- tab 2 content goes here -->
            </LinearLayout>

        </FrameLayout>

    </LinearLayout>

</TabHost>


No comments: