Android Tutorial

(avery) #1

By : Ketan Bhimani


254 

is set to 24 pixels. The time stamp used in this string can be used
to demonstrate when the view is created and that it’s not re-
created by simply changing tabs.

The flexibility of tabs that Android provides is great for adding
navigation to an application that has a bunch of views already
defined. Few changes, if any, need to be made to existing View and
Activity objects for them to work within the context of a TabHost.

Implementing Tabbing Without TabActivity

It is possible to design tabbed layouts without using the TabActivity
class. However, this requires a bit of knowledge about the
underpinnings of the TabHost and TabWidget controls. To define a
set of tabs within an XML layout file without using TabActivity,
begin with a TabHost (for example, TabHost1). Inside the TabHost,
include a vertically oriented LinearLayout that must contain a
TabWidget (which must have the id @android:id/tabs) and a
FrameLayout (which must have id @android :id /tabcontent). The
contents of each tab are then defined within the FrameLayout.

After you’ve defined the TabHost properly in XML, you must load
and initialize it using the TabHost setup() method on your activity’s
onCreate() method. First, you need to create a TabSpec for each
tab, setting the tab indicator using the setIndicator() method and
the tab content using the setContent() method. Next, add each tab
using the addTab() method of the TabHost. Finally, you should set
the default tab of the TabHost, using a method such as
setCurrentTabByTag().
Free download pdf