to top
Android APIs
public class

TabWidget

extends LinearLayout
implements View.OnFocusChangeListener
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.LinearLayout
         ↳ android.widget.TabWidget

Class Overview

Displays a list of tab labels representing each page in the parent's tab collection. The container object for this widget is TabHost. When the user selects a tab, this object sends a message to the parent container, TabHost, to tell it to switch the displayed page. You typically won't use many methods directly on this object. The container TabHost is used to add labels, add the callback handler, and manage callbacks. You might call this object to iterate the list of tabs, or to tweak the layout of the tab list, but most methods should be called on the containing TabHost object.

Summary

XML Attributes
Attribute Name Related Method Description
android:divider Drawable used to draw the divider between tabs. 
android:tabStripEnabled Determines whether the strip under the tab indicators is drawn or not. 
android:tabStripLeft Drawable used to draw the left part of the strip underneath the tabs. 
android:tabStripRight Drawable used to draw the right part of the strip underneath the tabs. 
[Expand]
Inherited XML Attributes
From class android.widget.LinearLayout
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Constants
From class android.widget.LinearLayout
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Fields
From class android.view.View
Public Constructors
TabWidget(Context context)
TabWidget(Context context, AttributeSet attrs)
TabWidget(Context context, AttributeSet attrs, int defStyle)
Public Methods
void addView(View child)

Adds a child view.

void childDrawableStateChanged(View child)
If addStatesFromChildren() is true, refreshes this group's drawable state (to include the states from its children).
void dispatchDraw(Canvas canvas)
Called by draw to draw the child views.
boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event)
Dispatches an AccessibilityEvent to the View first and then to its children for adding their text content to the event.
void focusCurrentTab(int index)
Sets the current tab and focuses the UI on it.
View getChildTabViewAt(int index)
Returns the tab indicator view at the given index.
int getTabCount()
Returns the number of tab indicator views.
boolean isStripEnabled()
Indicates whether the bottom strips on the tab indicators are drawn or not.
void onFocusChange(View v, boolean hasFocus)
Called when the focus state of a view has changed.
void onInitializeAccessibilityEvent(AccessibilityEvent event)
Initializes an AccessibilityEvent with information about this View which is the event source.
void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
Initializes an AccessibilityNodeInfo with information about this view.
void removeAllViews()
Call this method to remove all child views from the ViewGroup.
void sendAccessibilityEventUnchecked(AccessibilityEvent event)
This method behaves exactly as sendAccessibilityEvent(int) but takes as an argument an empty AccessibilityEvent and does not perform a check whether accessibility is enabled.
void setCurrentTab(int index)
Sets the current tab.
void setDividerDrawable(Drawable drawable)
Sets the drawable to use as a divider between the tab indicators.
void setDividerDrawable(int resId)
Sets the drawable to use as a divider between the tab indicators.
void setEnabled(boolean enabled)
Set the enabled state of this view.
void setLeftStripDrawable(Drawable drawable)
Sets the drawable to use as the left part of the strip below the tab indicators.
void setLeftStripDrawable(int resId)
Sets the drawable to use as the left part of the strip below the tab indicators.
void setRightStripDrawable(Drawable drawable)
Sets the drawable to use as the right part of the strip below the tab indicators.
void setRightStripDrawable(int resId)
Sets the drawable to use as the right part of the strip below the tab indicators.
void setStripEnabled(boolean stripEnabled)
Controls whether the bottom strips on the tab indicators are drawn or not.
Protected Methods
int getChildDrawingOrder(int childCount, int i)
Returns the index of the child to draw for this iteration.
void onSizeChanged(int w, int h, int oldw, int oldh)
This is called during layout when the size of this view has changed.
[Expand]
Inherited Methods
From class android.widget.LinearLayout
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.View.OnFocusChangeListener
From interface android.view.ViewManager
From interface android.view.ViewParent
From interface android.view.accessibility.AccessibilityEventSource

XML Attributes

android:divider

Drawable used to draw the divider between tabs.

May be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or "#aarrggbb".

This corresponds to the global attribute resource symbol divider.

Related Methods

android:tabStripEnabled

Determines whether the strip under the tab indicators is drawn or not.

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol tabStripEnabled.

Related Methods

android:tabStripLeft

Drawable used to draw the left part of the strip underneath the tabs.

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

This corresponds to the global attribute resource symbol tabStripLeft.

Related Methods

android:tabStripRight

Drawable used to draw the right part of the strip underneath the tabs.

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

This corresponds to the global attribute resource symbol tabStripRight.

Related Methods

Public Constructors

public TabWidget (Context context)

Added in API level 1

public TabWidget (Context context, AttributeSet attrs)

Added in API level 1

public TabWidget (Context context, AttributeSet attrs, int defStyle)

Added in API level 1

Public Methods

public void addView (View child)

Added in API level 1

Adds a child view. If no layout parameters are already set on the child, the default parameters for this ViewGroup are set on the child.

Note: do not invoke this method from draw(android.graphics.Canvas), onDraw(android.graphics.Canvas), dispatchDraw(android.graphics.Canvas) or any related method.

Parameters
child the child view to add

public void childDrawableStateChanged (View child)

Added in API level 1

If addStatesFromChildren() is true, refreshes this group's drawable state (to include the states from its children).

Parameters
child The child whose drawable state has changed.

public void dispatchDraw (Canvas canvas)

Added in API level 1

Called by draw to draw the child views. This may be overridden by derived classes to gain control just before its children are drawn (but after its own view has been drawn).

Parameters
canvas the canvas on which to draw the view

public boolean dispatchPopulateAccessibilityEvent (AccessibilityEvent event)

Added in API level 4

Dispatches an AccessibilityEvent to the View first and then to its children for adding their text content to the event. Note that the event text is populated in a separate dispatch path since we add to the event not only the text of the source but also the text of all its descendants. A typical implementation will call onPopulateAccessibilityEvent(AccessibilityEvent) on the this view and then call the dispatchPopulateAccessibilityEvent(AccessibilityEvent) on each child. Override this method if custom population of the event text content is required.

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its dispatchPopulateAccessibilityEvent(View, AccessibilityEvent) is responsible for handling this call.

Note: Accessibility events of certain types are not dispatched for populating the event text via this method. For details refer to AccessibilityEvent.

Parameters
event The event.
Returns
  • True if the event population was completed.

public void focusCurrentTab (int index)

Added in API level 1

Sets the current tab and focuses the UI on it. This method makes sure that the focused tab matches the selected tab, normally at setCurrentTab(int). Normally this would not be an issue if we go through the UI, since the UI is responsible for calling TabWidget.onFocusChanged(), but in the case where we are selecting the tab programmatically, we'll need to make sure focus keeps up.

Parameters
index The tab that you want focused (highlighted in orange) and selected (tab brought to the front of the widget)

public View getChildTabViewAt (int index)

Added in API level 4

Returns the tab indicator view at the given index.

Parameters
index the zero-based index of the tab indicator view to return
Returns
  • the tab indicator view at the given index

public int getTabCount ()

Added in API level 4

Returns the number of tab indicator views.

Returns
  • the number of tab indicator views.

public boolean isStripEnabled ()

Added in API level 8

Indicates whether the bottom strips on the tab indicators are drawn or not.

public void onFocusChange (View v, boolean hasFocus)

Added in API level 1

Called when the focus state of a view has changed.

Parameters
v The view whose state has changed.
hasFocus The new focus state of v.

public void onInitializeAccessibilityEvent (AccessibilityEvent event)

Added in API level 14

Initializes an AccessibilityEvent with information about this View which is the event source. In other words, the source of an accessibility event is the view whose state change triggered firing the event.

Example: Setting the password property of an event in addition to properties set by the super implementation:

 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
     super.onInitializeAccessibilityEvent(event);
     event.setPassword(true);
 }

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its onInitializeAccessibilityEvent(View, AccessibilityEvent) is responsible for handling this call.

Note: Always call the super implementation before adding information to the event, in case the default implementation has basic information to add.

Parameters
event The event to initialize.

public void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)

Added in API level 14

Initializes an AccessibilityNodeInfo with information about this view. The base implementation sets:

Subclasses should override this method, call the super implementation, and set additional attributes.

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo) is responsible for handling this call.

Parameters
info The instance to initialize.

public void removeAllViews ()

Added in API level 1

Call this method to remove all child views from the ViewGroup.

Note: do not invoke this method from draw(android.graphics.Canvas), onDraw(android.graphics.Canvas), dispatchDraw(android.graphics.Canvas) or any related method.

public void sendAccessibilityEventUnchecked (AccessibilityEvent event)

Added in API level 4

This method behaves exactly as sendAccessibilityEvent(int) but takes as an argument an empty AccessibilityEvent and does not perform a check whether accessibility is enabled.

If an View.AccessibilityDelegate has been specified via calling setAccessibilityDelegate(AccessibilityDelegate) its sendAccessibilityEventUnchecked(View, AccessibilityEvent) is responsible for handling this call.

Parameters
event The event to send.

public void setCurrentTab (int index)

Added in API level 1

Sets the current tab. This method is used to bring a tab to the front of the Widget, and is used to post to the rest of the UI that a different tab has been brought to the foreground. Note, this is separate from the traditional "focus" that is employed from the view logic. For instance, if we have a list in a tabbed view, a user may be navigating up and down the list, moving the UI focus (orange highlighting) through the list items. The cursor movement does not effect the "selected" tab though, because what is being scrolled through is all on the same tab. The selected tab only changes when we navigate between tabs (moving from the list view to the next tabbed view, in this example). To move both the focus AND the selected tab at once, please use setCurrentTab(int). Normally, the view logic takes care of adjusting the focus, so unless you're circumventing the UI, you'll probably just focus your interest here.

Parameters
index The tab that you want to indicate as the selected tab (tab brought to the front of the widget)

public void setDividerDrawable (Drawable drawable)

Added in API level 4

Sets the drawable to use as a divider between the tab indicators.

Parameters
drawable the divider drawable

public void setDividerDrawable (int resId)

Added in API level 4

Sets the drawable to use as a divider between the tab indicators.

Parameters
resId the resource identifier of the drawable to use as a divider.

public void setEnabled (boolean enabled)

Added in API level 1

Set the enabled state of this view. The interpretation of the enabled state varies by subclass.

Parameters
enabled True if this view is enabled, false otherwise.

public void setLeftStripDrawable (Drawable drawable)

Added in API level 8

Sets the drawable to use as the left part of the strip below the tab indicators.

Parameters
drawable the left strip drawable

public void setLeftStripDrawable (int resId)

Added in API level 8

Sets the drawable to use as the left part of the strip below the tab indicators.

Parameters
resId the resource identifier of the drawable to use as the left strip drawable

public void setRightStripDrawable (Drawable drawable)

Added in API level 8

Sets the drawable to use as the right part of the strip below the tab indicators.

Parameters
drawable the right strip drawable

public void setRightStripDrawable (int resId)

Added in API level 8

Sets the drawable to use as the right part of the strip below the tab indicators.

Parameters
resId the resource identifier of the drawable to use as the right strip drawable

public void setStripEnabled (boolean stripEnabled)

Added in API level 8

Controls whether the bottom strips on the tab indicators are drawn or not. The default is to draw them. If the user specifies a custom view for the tab indicators, then the TabHost class calls this method to disable drawing of the bottom strips.

Parameters
stripEnabled true if the bottom strips should be drawn.

Protected Methods

protected int getChildDrawingOrder (int childCount, int i)

Added in API level 1

Returns the index of the child to draw for this iteration. Override this if you want to change the drawing order of children. By default, it returns i.

NOTE: In order for this method to be called, you must enable child ordering first by calling setChildrenDrawingOrderEnabled(boolean).

Parameters
i The current iteration.
Returns
  • The index of the child to draw this iteration.

protected void onSizeChanged (int w, int h, int oldw, int oldh)

Added in API level 1

This is called during layout when the size of this view has changed. If you were just added to the view hierarchy, you're called with the old values of 0.

Parameters
w Current width of this view.
h Current height of this view.
oldw Old width of this view.
oldh Old height of this view.