Skip to content

Most visited

Recently visited

navigation

TabHost

public class TabHost
extends FrameLayout implements ViewTreeObserver.OnTouchModeChangeListener

java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ android.widget.TabHost
Known Direct Subclasses


Container for a tabbed window view. This object holds two children: a set of tab labels that the user clicks to select a specific tab, and a FrameLayout object that displays the contents of that page. The individual elements are typically controlled using this container object, rather than setting values on the child elements themselves.

Summary

Nested classes

interface TabHost.OnTabChangeListener

Interface definition for a callback to be invoked when tab changed  

interface TabHost.TabContentFactory

Makes the content of a tab when it is selected. 

class TabHost.TabSpec

A tab has a tab indicator, content, and a tag that is used to keep track of it. 

Inherited XML attributes

From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View

Inherited constants

From class android.view.ViewGroup
From class android.view.View

Inherited fields

From class android.view.View

Public constructors

TabHost(Context context)
TabHost(Context context, AttributeSet attrs)
TabHost(Context context, AttributeSet attrs, int defStyleAttr)
TabHost(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Public methods

void addTab(TabHost.TabSpec tabSpec)

Add a tab.

void clearAllTabs()

Removes all tabs from the tab widget associated with this tab host.

boolean dispatchKeyEvent(KeyEvent event)

Dispatch a key event to the next view on the focus path.

void dispatchWindowFocusChanged(boolean hasFocus)

Called when the window containing this view gains or loses window focus.

CharSequence getAccessibilityClassName()

Return the class name of this object to be used for accessibility purposes.

int getCurrentTab()

Returns the current tab.

String getCurrentTabTag()

Returns the tag for the current tab.

View getCurrentTabView()

Returns the view for the current tab.

View getCurrentView()
FrameLayout getTabContentView()

Get the FrameLayout which holds tab content

TabWidget getTabWidget()
TabHost.TabSpec newTabSpec(String tag)

Creates a new TabHost.TabSpec associated with this tab host.

void onTouchModeChanged(boolean isInTouchMode)

Callback method to be invoked when the touch mode changes.

void setCurrentTab(int index)
void setCurrentTabByTag(String tag)

Sets the current tab based on its tag.

void setOnTabChangedListener(TabHost.OnTabChangeListener l)

Register a callback to be invoked when the selected state of any of the items in this list changes

void setup()

Call setup() before adding tabs if loading TabHost using findViewById().

void setup(LocalActivityManager activityGroup)

If you are using setContent(android.content.Intent), this must be called since the activityGroup is needed to launch the local activity.

Inherited methods

From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.view.ViewParent
From interface android.view.ViewManager
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.accessibility.AccessibilityEventSource
From interface android.view.ViewTreeObserver.OnTouchModeChangeListener

Public constructors

TabHost

added in API level 1
TabHost (Context context)

Parameters
context Context

TabHost

added in API level 1
TabHost (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

TabHost

added in API level 21
TabHost (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

TabHost

added in API level 21
TabHost (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Public methods

addTab

added in API level 1
void addTab (TabHost.TabSpec tabSpec)

Add a tab.

Parameters
tabSpec TabHost.TabSpec: Specifies how to create the indicator and content.

Throws
IllegalArgumentException If the passed tab spec has null indicator strategy and / or null content strategy.

clearAllTabs

added in API level 1
void clearAllTabs ()

Removes all tabs from the tab widget associated with this tab host.

dispatchKeyEvent

added in API level 1
boolean dispatchKeyEvent (KeyEvent event)

Dispatch a key event to the next view on the focus path. This path runs from the top of the view tree down to the currently focused view. If this view has focus, it will dispatch to itself. Otherwise it will dispatch the next node down the focus path. This method also fires any key listeners.

Parameters
event KeyEvent: The key event to be dispatched.

Returns
boolean True if the event was handled, false otherwise.

dispatchWindowFocusChanged

added in API level 1
void dispatchWindowFocusChanged (boolean hasFocus)

Called when the window containing this view gains or loses window focus. ViewGroups should override to route to their children.

Parameters
hasFocus boolean: True if the window containing this view now has focus, false otherwise.

getAccessibilityClassName

added in API level 23
CharSequence getAccessibilityClassName ()

Return the class name of this object to be used for accessibility purposes. Subclasses should only override this if they are implementing something that should be seen as a completely new class of view when used by accessibility, unrelated to the class it is deriving from. This is used to fill in AccessibilityNodeInfo.setClassName.

Returns
CharSequence

getCurrentTab

added in API level 1
int getCurrentTab ()

Returns the current tab.

Returns
int the current tab, may be null if no tab is set as current

getCurrentTabTag

added in API level 1
String getCurrentTabTag ()

Returns the tag for the current tab.

Returns
String the tag for the current tab, may be null if no tab is set as current

getCurrentTabView

added in API level 1
View getCurrentTabView ()

Returns the view for the current tab.

Returns
View the view for the current tab, may be null if no tab is set as current

getCurrentView

added in API level 1
View getCurrentView ()

Returns
View

getTabContentView

added in API level 1
FrameLayout getTabContentView ()

Get the FrameLayout which holds tab content

Returns
FrameLayout

getTabWidget

added in API level 1
TabWidget getTabWidget ()

Returns
TabWidget

newTabSpec

added in API level 1
TabHost.TabSpec newTabSpec (String tag)

Creates a new TabHost.TabSpec associated with this tab host.

Parameters
tag String: tag for the tab specification, must be non-null

Returns
TabHost.TabSpec

This value will never be null.

Throws
IllegalArgumentException If the passed tag is null

onTouchModeChanged

added in API level 1
void onTouchModeChanged (boolean isInTouchMode)

Callback method to be invoked when the touch mode changes.

Parameters
isInTouchMode boolean: True if the view hierarchy is now in touch mode, false otherwise.

setCurrentTab

added in API level 1
void setCurrentTab (int index)

Parameters
index int

setCurrentTabByTag

added in API level 1
void setCurrentTabByTag (String tag)

Sets the current tab based on its tag.

Parameters
tag String: the tag for the tab to set as current

setOnTabChangedListener

added in API level 1
void setOnTabChangedListener (TabHost.OnTabChangeListener l)

Register a callback to be invoked when the selected state of any of the items in this list changes

Parameters
l TabHost.OnTabChangeListener: The callback that will run

setup

added in API level 1
void setup ()

Call setup() before adding tabs if loading TabHost using findViewById(). However: You do not need to call setup() after getTabHost() in TabActivity. Example:

mTabHost = (TabHost)findViewById(R.id.tabhost);
mTabHost.setup();
mTabHost.addTab(TAB_TAG_1, "Hello, world!", "Tab 1");

setup

added in API level 1
void setup (LocalActivityManager activityGroup)

If you are using setContent(android.content.Intent), this must be called since the activityGroup is needed to launch the local activity. This is done for you if you extend TabActivity.

Parameters
activityGroup LocalActivityManager: Used to launch activities for tab content.

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

Hooray!

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 one-minute survey?
Help us improve Android tools and documentation.