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

When using tabhosts, the main difference between a TabActivity and a simple Activity, is that you have to do some more setting up first.
- public class TabTestActivity extends Activity {
- LocalActivityManager mLocalActivityManager;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- TabHost tabHost = (TabHost) findViewById(R.id.tabhostscreen_tabhost);
- mLocalActivityManager = new LocalActivityManager(this, false);
- tabHost.setup(mLocalActivityManager);
- mLocalActivityManager.dispatchCreate(savedInstanceState); //after the tab's setup is called, you have to call this or it wont work
- TabHost.TabSpec spec;
- Intent intent;
- //set up your tabs here. It's easy to just do seperate activities for each tab, and link them in here.
- intent = new Intent().setClass(this, SomeActivity.class);
- spec = tabHost.newTabSpec("tagname1").setIndicator("tab indicator 1", getResources().getDrawable(R.drawable.icon)).setContent(intent);
- tabHost.addTab(spec);
- intent = new Intent().setClass(this, SomeOtherActivity.class);
- spec = tabHost.newTabSpec("tagname2").setIndicator("tab indicator 2").setContent(intent);
- tabHost.addTab(spec);
- }
- @Override
- protected void onPause() {
- super.onPause();
- mLocalActivityManager.dispatchPause(isFinishing()); //you have to manually dispatch the pause msg
- }
- @Override
- protected void onResume() {
- super.onResume();
- mLocalActivityManager.dispatchResume(); //you have to manually dispatch the resume msg
- }
- }
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:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.c
om/apk/res/android" - android:orientation="vertical" android:background="#ffffff"
- android:layout_width="fill_parent" android:layout_height="fill_parent"
- android:weightSum="1">
- <TabHost android:id="@+id/tabhostscreen_tabho
st" - android:layout_width="fill_parent" android:layout_height="fill_parent">
- <RelativeLayout android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:id="@+id/tabhostscreen_relat
iveLayout1" - android:layout_weight="1">
- <FrameLayout android:id="@android:id/tabcontent&q
uot; - android:layout_width="fill_parent" android:layout_above="@android:id/tabs"
- android:layout_alignParentTop="true" android:layout_height="fill_parent"></FrameLayout>
- <TabWidget android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:id="@android:id/tabs"
- android:layout_alignParentBottom="true"></TabWidget>
- </RelativeLayout>
- </TabHost>
- </LinearLayout>
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@Idevicegazette (iDevice Gazette)
GSM-to-Skype bridge lets you lose those roaming fees http://bit.ly/lbRJeh #android
11 years 6 weeks ago -
@tommy_banane (tom b.)
RT @AndroidFavorite: #Android New Desktop Android Market Is Live, Adds Several New Features http://zorr0.nl/lFwXNz
11 years 6 weeks ago -
@dwilliams5 (Dennis Williams)
just completed a runtastic run of 3.02 km in 40 min 11 s with #runtastic #Android App: http://tinyurl.com/5tvrpe3
11 years 6 weeks ago -
@S_Pinz (Spinz!)
RT @Androidheadline: Out of box #LG Optimus 3D got Quadrant 2420 score. Thanks @blink_c #io2011 #android http://twitpic.com/4whkdz
11 years 6 weeks ago -
@tayaitapps (Taya IT)
Next Google TV Looks A Lot Like Android http://t.co/dvlTim3 via @alleyinsider #google #apple #android #tv #honeycomb
11 years 6 weeks ago
Poll
Useful resources
Android Development Projects
- iOS/Android Developer to take older Games and bring them Current
- Android apps developer - need to finish urgent.
- Buliding MobileApp For onlie order
- looking for android APP developers
- Create an ecommerce app
- text-to voice for smartphones IOS - GOOGLE - HARMONY - AND ALEXA
- Optimize Images on App
- Create small feature with drag-drop text for Android
- Scouting for advanced website and Mobile apps developers. Potential Long-term contract.
- BLACK SCREEN