TabHost outside a TabActivity


SDK Version: 
M3

I found no really easy to understand example on tabhosts, when you don't want to use a TabActivity, so here is one.

tabs

When using tabhosts, the main difference between a TabActivity and a simple Activity, is that you have to do some more setting up first.

  1. public class TabTestActivity extends Activity {
  2.         LocalActivityManager mLocalActivityManager;
  3.  
  4.         @Override
  5.         public void onCreate(Bundle savedInstanceState) {
  6.                 super.onCreate(savedInstanceState);
  7.                 setContentView(R.layout.main);
  8.                
  9.                 TabHost tabHost = (TabHost) findViewById(R.id.tabhostscreen_tabhost);
  10.                 mLocalActivityManager = new LocalActivityManager(this, false);
  11.  
  12.                 tabHost.setup(mLocalActivityManager);
  13.                 mLocalActivityManager.dispatchCreate(savedInstanceState); //after the tab's setup is called, you have to call this or it wont work
  14.  
  15.                 TabHost.TabSpec spec;
  16.                 Intent intent;
  17.                 //set up your tabs here. It's easy to just do seperate activities for each tab, and link them in here.
  18.                 intent = new Intent().setClass(this, SomeActivity.class);
  19.                 spec = tabHost.newTabSpec("tagname1").setIndicator("tab indicator 1", getResources().getDrawable(R.drawable.icon)).setContent(intent);
  20.                 tabHost.addTab(spec);
  21.  
  22.                 intent = new Intent().setClass(this, SomeOtherActivity.class);
  23.                 spec = tabHost.newTabSpec("tagname2").setIndicator("tab indicator 2").setContent(intent);
  24.                 tabHost.addTab(spec);
  25.         }
  26.  
  27.         @Override
  28.         protected void onPause() {
  29.                 super.onPause();
  30.                 mLocalActivityManager.dispatchPause(isFinishing()); //you have to manually dispatch the pause msg
  31.         }
  32.  
  33.         @Override
  34.         protected void onResume() {
  35.                 super.onResume();
  36.                 mLocalActivityManager.dispatchResume(); //you have to manually dispatch the resume msg
  37.         }
  38. }

The layout I used for this tutorial, has tabs on the bottom, but you could reorganise it pretty easily, just swap the FrameLayout with the TabWidget.

The layout file:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:orientation="vertical" android:background="#ffffff"
  4.         android:layout_width="fill_parent" android:layout_height="fill_parent"
  5.         android:weightSum="1">
  6.         <TabHost android:id="@+id/tabhostscreen_tabhost"
  7.                 android:layout_width="fill_parent" android:layout_height="fill_parent">
  8.                 <RelativeLayout android:layout_width="fill_parent"
  9.                         android:layout_height="wrap_content" android:id="@+id/tabhostscreen_relativeLayout1"
  10.                         android:layout_weight="1">
  11.                         <FrameLayout android:id="@android:id/tabcontent&quot;
  12.                                 android:layout_width="fill_parent" android:layout_above="@android:id/tabs"
  13.                                 android:layout_alignParentTop="true" android:layout_height="fill_parent"></FrameLayout>
  14.                         <TabWidget android:layout_width="fill_parent"
  15.                                 android:layout_height="wrap_content" android:id="@android:id/tabs"
  16.                                 android:layout_alignParentBottom="true"></TabWidget>
  17.                 </RelativeLayout>
  18.         </TabHost>
  19. </LinearLayout>