The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.
Before we had the action bar and
ViewPager, we had
TabWidget as our means
of displaying tabs. Nowadays, in most cases, using
tabs with a
ViewPager is the preferred option.
However, there may be cases where the classic tabs are a better solution, or you may
have inherited legacy code that still uses
ListActivity helps one use a
TabActivity helps one use a
TabActivity is marked as deprecated. That is largely because
its parent class,
ActivityGroup, is deprecated. While you can still use
it is no longer recommended. It also is not necessary, as there are ways to use
TabWidget without using
TabActivity, as will be demonstrated later
in this chapter.
There are a few widgets and containers you need to use in order to set up a tabbed portion of a view:
TabHostis the overarching container for the tab buttons and tab contents
TabWidgetimplements the row of tab buttons, which contain text labels and optionally contain icons
FrameLayoutis the container for the tab contents; each tab content is a child of the
You load contents into that
FrameLayout in one of two ways:
FrameLayoutin a layout XML file you are using for the whole tab setup
Curiously, you do not define what goes in the tabs themselves, or how they tie to the
content, in the layout XML file. Instead, you must do that in Java, by creating a series
TabSpec objects (obtained via
TabHost), configuring them, then
adding them in sequence to the
The two key methods on
setContent(), where you indicate what goes in the tab content for this tab, typically the
android:idof the view you want shown when this tab is selected
setIndicator(), where you provide the caption for the tab button and, in some flavors of this method, supply a
Drawableto represent the icon for the tab
Note that tab “indicators” can actually be views in their own right, if you need more control than a simple label and optional icon.
Also note that you must call
setup() on the
TabHost before configuring any
TabSpec objects. The call to
setup() is not needed if you are using
TabActivity base class for your activity.
The preview of this section is unavailable right now, but if you leave your name and number at the sound of the tone, it might get back to you (BEEEEEEEEEEEEP!).
The preview of this section is off trying to sweet-talk the Khaleesi into providing us with a dragon.