Lost Password?

A password will be emailed to you. You will be able to change your password and other profile details once you have logged in.

Adding Tabs Dynamically

The TabWidget in Android is set up to allow you to easily define tabs at compile time. However, sometimes, you want to add tabs to your activity during runtime. For example, imagine an email client where individual emails get opened in their own tab, for easy toggling between messages. In this case, you don't know how many tabs or what their contents will be until runtime, when the user chooses to open a message.

Fortunately, Android also supports adding tabs dynamically at runtime, and this blog post will show you how.

For compile-time-defined tabs, in your Java code, to set up tabs with contents provided in layout XML, you simply create a TabHost.TabSpec object and call setContent() on it, with the widget ID of the tab's contents:

[sourcecode language='java']
TabHost.TabSpec spec=tabs.newTabSpec("buttontab");

spec.setContent(R.id.buttontab);
spec.setIndicator("Button");
tabs.addTab(spec);
[/sourcecode]

Adding tabs dynamically at runtime works much the same way, except you use a different flavor of setContent(), one that takes a TabHost.TabContentFactory instance. This is just a callback that will be invoked -- you provide an implementation of createTabContent() and use it to build and return the View that becomes the content of the tab.

Let's take a look at an example.

First, here is some layout XML for an activity that sets up the tabs and defines one tab, containing a single button:

[sourcecode language='xml']

android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="62px">

Mark Murphy is the founder of CommonsWare and is the author of _The Busy Coder's Guide to Android Development_, _The Busy Coder's Guide to *Advanced* Android Development_, and _Android Programming Tutorials_.

Like this? Share it.

Related Posts

Ubuntu Ported To the Galaxy S II

  • 8

The Galaxy S II isn't even officially available yet in the US but it has already been rooted, overclocked and the first custom ROMs are coming up as well. I used to run Ubuntu on my original Galaxy S (Just for fun of course, like you can effectively use Ubuntu on a 4" screen), so I decided to port Ubuntu to the Galaxy S II. Read More »

How To Modify .apk Files

  • 13

The extension Google uses for Android applications may seem a bit complicated, but it really isn't. In fact, an .apk is nothing else than a .zip file disguised as an .apk. That makes things real simple. Read More »

  • zach

    how about removing tabs? that would be a nice addition to the sdk...

  • Abhishek Dixit

    It will good if u can please provide with an example to tell how to start an Activity ina new Tab.

  • Paul R

    Just wanted to mention that I had to capitalize the xml elements in order to get it to work.

  • jrcsrc

    Is it possible to reuse same activity class for two tabs?

    I have tried to do so....OnCreate of activity called twice but onDestroy called once and crashed !!

    Any Idea weather we can use same activity class two or more time for multiple tabs in TabActivity?

    Thanks,
    jrc.src