Skip to content

Most visited

Recently visited


Using the Version-Aware Component

Now that you have two implementations of TabHelper and CompatTab—one for Android 3.0 and later and one for earlier versions of the platform—it's time to do something with these implementations. This lesson discusses creating the logic for switching between these implementations, creating version-aware layouts, and finally using the backward-compatible UI component.

Add the Switching Logic

The TabHelper abstract class acts as a factory for creating version-appropriate TabHelper and CompatTab instances, based on the current device's platform version:

public abstract class TabHelper {
    // Usage is TabHelper.createInstance(activity)
    public static TabHelper createInstance(FragmentActivity activity) {
            return new TabHelperHoneycomb(activity);
        } else {
            return new TabHelperEclair(activity);

    // Usage is mTabHelper.newTab("tag")
    public CompatTab newTab(String tag) {
            return new CompatTabHoneycomb(mActivity, tag);
        } else {
            return new CompatTabEclair(mActivity, tag);

Create a Version-Aware Activity Layout

The next step is to provide layouts for your activity that can support the two tab implementations. For the older implementation (TabHelperEclair), you need to ensure that your activity layout contains a TabWidget and TabHost, along with a container for tab contents:


<!-- This layout is for API level 5-10 only. -->
<TabHost xmlns:android=""


            android:layout_height="wrap_content" />

            android:layout_weight="1" />


For the TabHelperHoneycomb implementation, all you need is a FrameLayout to contain the tab contents, since the tab indicators are provided by the ActionBar:


<FrameLayout xmlns:android=""
    android:layout_height="match_parent" />

At runtime, Android will decide which version of the main.xml layout to inflate depending on the platform version. This is the same logic shown in the previous section to determine which TabHelper implementation to use.

Use TabHelper in Your Activity

In your activity's onCreate() method, you can obtain a TabHelper object and add tabs with the following code:

public void onCreate(Bundle savedInstanceState) {

    TabHelper tabHelper = TabHelper.createInstance(this);

    CompatTab photosTab = tabHelper

    CompatTab videosTab = tabHelper

When running the application, this code inflates the correct activity layout and instantiates either a TabHelperHoneycomb or TabHelperEclair object. The concrete class that's actually used is opaque to the activity, since they share the common TabHelper interface.

Below are two screenshots of this implementation running on an Android 2.3 and Android 4.0 device.

Example screenshot of tabs running on an Android 2.3 device (using TabHelperEclair). Example screenshots of tabs running on an Android 4.0 device (using TabHelperHoneycomb).

Figure 1. Example screenshots of backward-compatible tabs running on an Android 2.3 device (using TabHelperEclair) and an Android 4.0 device (using TabHelperHoneycomb).

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields


Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)