Android Tutorial

(avery) #1
Android Tutorial 251

Using TabActivity

A screen layout with tabs consists of a TabActivity and a TabHost.
The TabHost consists of TabSpecs, a nested class of TabHost, which
contains the tab information including the tab title and the contents
of the tab. The contents of the tab can either be a predefined View,
an Activity launched through an Intent object, or the View can be
created with a factory provided by an implementation of
TabContentFactory.

Tabs aren’t as complex as they might sound at first. Each tab is
effectively a container for a View. That View can come from any
View that is ready to be shown, such as an XML layout file.
Alternatively, that View can come from launching an Activity. The
following example demonstrates each of these methods using View
objects and Activity objects created in the previous examples of
this chapter:

public class TabLayout extends TabActivity implements
android.control.TabHost.TabContentFactory {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost();
LayoutInflater.from(this).inflate(
R.layout.example_layout,tabHost.getTabContentView(), true);
tabHost.addTab(tabHost.newTabSpec(“tab1”).setIndicator
(“Grid”).setContent(new Intent(this, GridLayout.class)));
tabHost.addTab(tabHost.newTabSpec(“tab2”).setIndicator
(“List”).setContent(new Intent(this, List.class)));
tabHost.addTab(tabHost.newTabSpec(“tab3”).setIndicator
(“Basic”).setContent(R.id.two_texts));
tabHost.addTab(tabHost.newTabSpec(“tab4”).setIndicator
(“Factory”).setContent(this));
}
public View createTabContent(String tag) {
if (tag.compareTo(“tab4”) == 0) {

Free download pdf