A
TabActivity
and its
TabHost
view
are the Android equivalent of the iPhone
UITabBarController
.
I created this app from
Android’s
tab
tutorial.
The
indicators
(labels)
start at 1 because human beings like to count starting at 1,
but Android counts the tabs starting at 0.
Tab 1 (using Android’s counting scheme) has an
icon
and Tab 2 is initially the
current
tab.
This main
Activity
of this app has the attribute
android:theme="@android:style/Theme.NoTitleBar"
AndroidManifest.xml
.ActionBar
,
and allowed the tab icon(s) to appear.
The snapshot of
ApiDemos/Views/Tabs
ActionBar
,
and the icons were not shown.
See the
next example
for icons in the
ActionBar
tabs,
and for avoiding the now-deprecated class
TabActivity
.
Each tab launches a different activity.
The main activity must be derived from class
TabActivity
(not from class plain old
Activity
),
which has a
getTabHost
method.
The
TabHost
view contains two subviews: a
TabWidget
containing the tabs and a
FrameLayout
containing the views that are presented by the tabs.
(We saw
FrameLayout
here.)
If you need them,
these two subviews are returned by the
getTabWidget
and
getTabContentView
methods of the
TabHost
.
MyTabActivity.java
Tab1Activity.java
Tab2Activity.java
Tab3Activity.java
main.xml
:
a
TabHost
containing a
TabWidget
and a
FrameLayout
.
The
id
numbers
android.R.id.tabs
)android.R.id.tabcontent
)ic_tab2.xml
contains a
State
List:
selected and unselected.
ic_tab2_grey.png
:
selected, 32 × 32 pixels.
ic_tab2_white.png
:
unselected.
AndroidManifest.xml
:
the
activity
element has the attribute
android:theme="@android:style/Theme.NoTitleBar"
.TabActivity
is already taken,
so I named my subclass
MyTabActivity
.
ic_tab2.xml
res/drawable
.TabActivity
.
The other three Activities should be subclasses of
Activity
.
src
folder.
android.app.Activity
activity
elements to
the
application
element in
AndroidManifest.xml
.android:theme="@android:style/Theme.NoTitleBar"to the first
activity
element in
AndroidManifest.xml
.onCreate
.
final Class<?>[] c = { //an array of Class objects. "class" is a Java keyword. Tab1Activity.class, Tab2Activity.class, Tab3Activity.class }; for (int i = 0; i < c.length; ++i) { TabHost.TabSpec tabSpec = tabHost.newTabSpec(String.format("tag%d", i + 1)); tabSpec.setIndicator(String.format("Tab%d", i + 1)); tabSpec.setContent(new Intent(this, c[i])); tabHost.addTab(tabSpec); }
tabSpec.setIndicator(String.format("Tab%d", i + 1)) .setContent(new Intent(this, c[i]));
Tab1Activity
create a
ToggleButton
instead of a
TextView
.
The newly-created
ToggleButton
is initialized to off.
ToggleButton toggleButton = new ToggleButton(this); setContentView(toggleButton);Observe that when you go to another tab and come back to tab 1, the state of the
ToggleButton
is preserved.
This means that you are returning to the same
Tab1Activity
object.
Now add a flag to the
Intent
object before installing it into tab 1.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);Observe that the
onCreate
onCreate
method of
Tab1Activity
is now called every time you go back to tab 1.
The
ToggleButton
is always off when it reappears.
TabHost
an
onTabChangeListener
that will detect when a tab has been selected.
TabWidget
below the
FrameLayout
in the
LinearLayout
in
main.xml
.
Give the following properties to the
FrameLayout
.
android:layout_height="wrap_content" android:layout_weight="1"Be sure to keep the names of the id properties
tabs
and
tabcontent
.setContentView
in
MyTabActivity
.
Why do we still see the picture?
And why don’t the
android:id
Tabs1.java
.TabActivity
.
No
ActionBar
or
Fragment
s.
ActionBar
holds tabs.
Each tab holds a
TabListener
.
Each
TabListener
holds a
Fragment
.
Each
Fragment
holds a
View
.
ActionBarTabs.java
,action_bar_tabs.xml
,action_bar_tab_content.xml
FragmentTabs.java
The
onCreate
in the following ApiDemos
does not call
setContentView
.
Instead,
we inflate the layout in the
.xml
file with the
Activity’s
LayoutInflater
.
The inflator uses the inflated layout to fill up the tabhost’s empty
tab
content view.
The third ApiDemo has multiple Activities.
TextView
in an
.xml
file.
Tabs1.java
tabs1.xml
:
three
TextView
s
in a
FrameLayout
.strings.xml
colors.xml
:
blue, red, green.TextView
created by the Activity.
The Activity implements the interface
TabHost.TabContentFactory
containing the method
createTabContent
.
Tabs2.java
star_big_on.png
(high).
See the
Icon
Design Guidelines.star_big_on.png
(medium)Activity
.
Construct
an
Intent
object and tell it what subclass of Activity you want to create.
Pass the Intent object to the
setContent
method
of the
TabHost.TabSpec
.
Tabs3.java
List1.java
is a
ListActivity
with a
ListView
and an
ArrayAdapter<String>
.
Cheeses.java
,
used by class
List1
.List8.java
is a
ListActivity
with a
ListView
and a
PhotoAdapter
(defined in the same file).
Controls1.java
:
the destroy tab instantiates
a plain old
Activity
with lots of widgets.
LayoutInflater.from(this).inflate(R.layout.tabs1, tabHost.getTabContentView(), true);
LayoutInflater inflater = LayoutInflater.from(this); inflater.inflate(R.layout.tabs1, tabHost.getTabContentView(), true);
TabHost tabHost = getTabHost(); tabHost.addTab(tabHost.newTabSpec("tab1") .setIndicator("tab1") .setContent(R.id.view1));
TabHost tabHost = getTabHost(); TabHost.TabSpec tabSpec = tabHost.newTabSpec("tab1"); tabSpec.setIndicator("tab1"); tabSpec.setContent(R.id.view1); tabHost.addTab(tabSpec);