Tab Layout with TabHost
This article will provide a high level overview of the the TabHost, an older API used to create tabbed layouts in a Xamarin.Android application.
TabHost is an old API that has been deprecated by Google. Developers are encouraged to
build tabbed applications using the ActionBar. The
ActionBar is available in all version of Android. It was first introduced in Android 3.0 (API level 11)
and was back ported to Android 2.2 (API level 8) and Android 2.3 (API level 10) in
the V7 AppCompat Library, which
is available to Xamarin.Android via the
Xamarin Android Support Library - V7
TabHost is the older, original API for creating tabbed user
interfacesIt is best suited for Xamarin.Android Applications that must
support Android 2.2 and Android 2.3 and cannot use ActionBarSherlock.
The following five components are all involved with the
TabActivity – This is a specialized view that acts as a container for a
TabHost – This is a container for the tabbed UI. It hosts two children: a list of tab labels and a
FrameLayoutthat displays the tab content.
TabWidget – This control displays a list of tab labels, one for each tab in the
TabHost. Each tab in a
TabHostare represented by a
TabHost.TabSpecobject. This object contains the meta-data for each tab. When the user selects a tab, the
TabHostresponds to the selection by displaying the appropriate tab.
FrameLayout – A tabbed UI must have a
FrameLayoutcontained inside of a
TabHost. It is used to display the contents for the tab.
Activities or Views – When a tab is selected, it displays either an Activity or a View in the
The following diagram shows how all of these components relate together:
The tab content may be either Activities or Views. Views are relatively lightweight and simple, but may result in a lot of unrelated code co-habitating in the Activity. This will result in a poor separation of concerns and a bloated class that is hard to maintain. In contrast, Activities require system resources but allow for a more modular approach with the logic for each tab encapsulated in it's own distinct class.
This article explained the high level components of the older
API for Android and how they all relate together.