Jeff Sharkey



Android TabHost in the M5 SDK

So about a week ago I wrote up a quick example for TabHost. Then yesterday Google released a new version of the Android SDK which changed quite a bit. In the process, TabHost is no longer deprecated (yay!), but its interface has changed quite a bit, so I spent about an hour changing my earlier example to work.

Here is a quick run-down of what applies to TabHost: all of our id’s change to android:id’s, TabWidget’s width needs to fill_parent, and its height needs to be about 65px. The top padding of the content FrameLayout also needs to be about 65px.

The Java interface to TabHost also changed, letting us put both text and icons in the tabs:

setContentView(R.layout.tabs);

TabHost tabs = (TabHost)this.findViewById(R.id.tabs);
tabs.setup();

TabHost.TabSpec one = tabs.newTabSpec(“one”);
one.setContent(R.id.content1);
one.setIndicator(“labelone”, this.getResources().getDrawable(R.drawable.gohome));
tabs.addTab(one);

TabHost.TabSpec two = tabs.newTabSpec(“two”);
two.setContent(R.id.content2);
two.setIndicator(“labeltwo”);
tabs.addTab(two);

tabs.setCurrentTab(0);

And here’s a peek at what the resulting output looks like:

The one nice feature is that the tabs are now clickable instead of just keypad-only.  That’s about it, so have fun with TabHost in the new SDK. :) The home icon above came from the Tango Icon Library.