TabWidget


public class TabWidget
extends LinearLayout implements View.OnFocusChangeListener

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


This class was deprecated in API level 30.
new applications should use fragment APIs instead of this class: Use TabLayout and ViewPager instead.

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

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. 

Inherited XML attributes

Inherited constants

Inherited fields

Public constructors

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

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.

void focusCurrentTab(int index)

Sets the current tab and focuses the UI on it.

CharSequence getAccessibilityClassName()

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

View getChildTabViewAt(int index)

Returns the tab indicator view at the given index.

Drawable getLeftStripDrawable()
Drawable getRightStripDrawable()
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.

PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex)

Resolve the pointer icon that should be used for specified pointer in the motion event.

void removeAllViews()

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

void setCurrentTab(int index)

Sets the current tab.

void setDividerDrawable(int resId)

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

void setDividerDrawable(Drawable drawable)

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(int resId)

Sets the drawable to use as the right 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 setStripEnabled(boolean stripEnabled)

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

Protected methods

int getChildDrawingOrder(int childCount, int i)

Converts drawing order position to container position.

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

This is called during layout when the size of this view has changed.

Inherited methods

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 a theme attribute in the form "?[package:]type/name".

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

Related methods:

android:tabStripEnabled

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

May be a boolean value, such as "true" or "false".

android:tabStripLeft

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

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

Related methods:

android:tabStripRight

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

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

Related methods:

Public constructors

TabWidget

Added in API level 1
public TabWidget (Context context)

Parameters
context Context

TabWidget

Added in API level 1
public TabWidget (Context context, 
                AttributeSet attrs)

Parameters
context Context

attrs AttributeSet

TabWidget

Added in API level 1
public TabWidget (Context context, 
                AttributeSet attrs, 
                int defStyleAttr)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

TabWidget

Added in API level 21
public TabWidget (Context context, 
                AttributeSet attrs, 
                int defStyleAttr, 
                int defStyleRes)

Parameters
context Context

attrs AttributeSet

defStyleAttr int

defStyleRes int

Public methods

addView

Added in API level 1
public void addView (View child)

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 View.draw(android.graphics.Canvas), View.onDraw(android.graphics.Canvas), dispatchDraw(android.graphics.Canvas) or any related method.

Parameters
child View: the child view to add

childDrawableStateChanged

Added in API level 1
public void childDrawableStateChanged (View child)

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

Parameters
child View: The child whose drawable state has changed. This value cannot be null.

dispatchDraw

Added in API level 1
public void dispatchDraw (Canvas canvas)

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 Canvas: This value cannot be null.

focusCurrentTab

Added in API level 1
public void focusCurrentTab (int index)

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 int: The tab that you want focused (highlighted in orange) and selected (tab brought to the front of the widget)

See also:

getAccessibilityClassName

Added in API level 23
Deprecated in API level 30
public 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

getChildTabViewAt

Added in API level 4
Deprecated in API level 30
public View getChildTabViewAt (int index)

Returns the tab indicator view at the given index.

Parameters
index int: the zero-based index of the tab indicator view to return

Returns
View the tab indicator view at the given index

getLeftStripDrawable

Added in API level 24
Deprecated in API level 30
public Drawable getLeftStripDrawable ()

Related XML Attributes:

Returns
Drawable the drawable used as the left part of the strip below the tab indicators, may be null

getRightStripDrawable

Added in API level 24
Deprecated in API level 30
public Drawable getRightStripDrawable ()

Related XML Attributes:

Returns
Drawable the drawable used as the right part of the strip below the tab indicators, may be null

getTabCount

Added in API level 4
Deprecated in API level 30
public int getTabCount ()

Returns the number of tab indicator views.

Returns
int the number of tab indicator views

isStripEnabled

Added in API level 8
Deprecated in API level 30
public boolean isStripEnabled ()

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

Returns
boolean

onFocusChange

Added in API level 1
public void onFocusChange (View v, 
                boolean hasFocus)

Called when the focus state of a view has changed.

Parameters
v View: The view whose state has changed.

hasFocus boolean: The new focus state of v.

onResolvePointerIcon

Added in API level 24
Deprecated in API level 30
public PointerIcon onResolvePointerIcon (MotionEvent event, 
                int pointerIndex)

Resolve the pointer icon that should be used for specified pointer in the motion event. The default implementation will resolve the pointer icon to one set using setPointerIcon(android.view.PointerIcon) for mouse devices. Subclasses may override this to customize the icon for the given pointer. For example, to always show the PointerIcon.TYPE_HANDWRITING icon for a stylus pointer, the event can be resolved in the following way:

 @Override
 public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) {
     final int toolType = event.getToolType(pointerIndex);
     if (!event.isFromSource(InputDevice.SOURCE_MOUSE)
             && event.isFromSource(InputDevice.SOURCE_STYLUS)
             && (toolType == MotionEvent.TOOL_TYPE_STYLUS
                     || toolType == MotionEvent.TOOL_TYPE_ERASER)) {
         // Show this pointer icon only if this pointer is a stylus.
         return PointerIcon.getSystemIcon(mContext, PointerIcon.TYPE_HANDWRITING);
     }
     // Use the default logic for determining the pointer icon for other non-stylus pointers,
     // like for the mouse cursor.
     return super.onResolvePointerIcon(event, pointerIndex);
 }
 

Parameters
event MotionEvent: The MotionEvent that requires a pointer icon to be resolved for one of pointers.

pointerIndex int: The index of the pointer in event for which to retrieve the PointerIcon. This will be between 0 and MotionEvent.getPointerCount().

Returns
PointerIcon the pointer icon to use for specified pointer, or null if a pointer icon is not specified and the default icon should be used.

removeAllViews

Added in API level 1
public void removeAllViews ()

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

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

setCurrentTab

Added in API level 1
public void setCurrentTab (int index)

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 focusCurrentTab(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 int: the index of the tab that you want to indicate as the selected tab (tab brought to the front of the widget)

setDividerDrawable

Added in API level 4
Deprecated in API level 30
public void setDividerDrawable (int resId)

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

Related XML Attributes:

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

setDividerDrawable

Added in API level 4
Deprecated in API level 30
public void setDividerDrawable (Drawable drawable)

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

Related XML Attributes:

Parameters
drawable Drawable: the divider drawable This value may be null.

setEnabled

Added in API level 1
public void setEnabled (boolean enabled)

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

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

setLeftStripDrawable

Added in API level 8
Deprecated in API level 30
public void setLeftStripDrawable (Drawable drawable)

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

Related XML Attributes:

Parameters
drawable Drawable: the left strip drawable This value may be null.

setLeftStripDrawable

Added in API level 8
Deprecated in API level 30
public void setLeftStripDrawable (int resId)

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

Related XML Attributes:

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

setRightStripDrawable

Added in API level 8
Deprecated in API level 30
public void setRightStripDrawable (int resId)

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

Related XML Attributes:

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

setRightStripDrawable

Added in API level 8
Deprecated in API level 30
public void setRightStripDrawable (Drawable drawable)

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

Related XML Attributes:

Parameters
drawable Drawable: the right strip drawable This value may be null.

setStripEnabled

Added in API level 8
Deprecated in API level 30
public void setStripEnabled (boolean stripEnabled)

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 boolean: true if the bottom strips should be drawn.

Protected methods

getChildDrawingOrder

Added in API level 1
protected int getChildDrawingOrder (int childCount, 
                int i)

Converts drawing order position to container position. Override this if you want to change the drawing order of children. By default, it returns drawingPosition.

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

Parameters
childCount int

i int: the drawing order position.

Returns
int the container position of a child for this drawing order position.

onSizeChanged

Added in API level 1
protected void onSizeChanged (int w, 
                int h, 
                int oldw, 
                int oldh)

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 int: Current width of this view.

h int: Current height of this view.

oldw int: Old width of this view.

oldh int: Old height of this view.