For an application that I am writing, I had to use tabs for part of the interface. Quite honestly, this proved much more difficult then I anticipated; tabs in Android are…. weird, to say the least. Thus I decided to post a blog entry describing how to use them. I will save the customization entry for a later entry.
First item to note, the root ViewGroup: TabHost. This does not render any UI, persay, it merely informs the activity that it will be hosting n activities, for each tab. the TabWidget represents where the tabs themselves will be displayed. The FrameLayout is where the content goes. In so far as I have been able to tell, it is easiest to use these default names and have your underlying Activity inherit from TabActivity.
Next, the id values that I am using here are, for what I can tell, required; as is the syntax. You will see how to access these components in the Activity code.
getTabHost() is a method that is implemented by TabActivity which will return the TabHost, assuming it has the name tabhost. For each tab, an activity will be loaded into FrameLayout, hence the use of the Intents above; that being the case, do not forget to register your activities with your manifest file. Running this code will produce the following:
Notice that there is some empty space above the “content” that we set. This is because the default tabs expect an icon to be placed here. Without it, as you can see, it looks rather odd. I wont cover customizing tabs to that extent in this entry, but definitely in the next, as understanding it is critical to making tabs look good within a custom application.
The activities can contain pretty much anything you like. One thing to notice is that we used a LinearLayout to separate the TabWidget and FrameLayout. The layout you see above is not at all predefined, you can place the TabWidget and FrameLayout anywhere on the page with any number of other views. For example:
Come to think of the TabWidget and FrameLayout as “hot zones”, that is places where the specialized effect resides and that can be used in a layout; if that makes any sense
So at this point, I think you can see how tabs can be used within your application. Obviously, understanding them to this point is not going to do you well except for the most basic of applications. So to help you forward I will say this: to truly customize your tabs, you will need to targeting Android 2.x, and using the updated setContent call which takes a type View as a parameter.
I am including the source code for this version of the example application, I will be using it again in the next entry when I take this example further with Tab customization.