TabLayout

public class TabLayout


TabLayout provides a horizontal layout to display tabs.

Population of the tabs to display is done through Tab instances. You create tabs via newTab. From there you can change the tab's label or icon via setText and setIcon respectively. To display the tab, you need to add it to the layout via one of the addTab methods. For example:

TabLayout tabLayout = ...;
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
You should add a listener via addOnTabSelectedListener to be notified when any tab's selection state has been changed.

You can also add items to TabLayout in your layout through the use of TabItem. An example usage is like so:

<com.google.android.material.tabs.TabLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

    <com.google.android.material.tabs.TabItem
            android:text="@string/tab_text"/>

    <com.google.android.material.tabs.TabItem
            android:icon="@drawable/ic_android"/>

</com.google.android.material.tabs.TabLayout>

ViewPager integration

If you're using a androidx.viewpager.widget.ViewPager together with this layout, you can call setupWithViewPager to link the two together. This layout will be automatically populated from the PagerAdapter's page titles.

This view also supports being used as part of a ViewPager's decor, and can be added directly to the ViewPager in a layout resource file like so:

<androidx.viewpager.widget.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.tabs.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top" />

</androidx.viewpager.widget.ViewPager>

For more information, see the component developer guidance and design guidelines.

ref com.google.android.material.R.styleable#TabLayout_tabPadding

ref com.google.android.material.R.styleable#TabLayout_tabPaddingStart

ref com.google.android.material.R.styleable#TabLayout_tabPaddingTop

ref com.google.android.material.R.styleable#TabLayout_tabPaddingEnd

ref com.google.android.material.R.styleable#TabLayout_tabPaddingBottom

ref com.google.android.material.R.styleable#TabLayout_tabContentStart

ref com.google.android.material.R.styleable#TabLayout_tabBackground

ref com.google.android.material.R.styleable#TabLayout_tabMinWidth

ref com.google.android.material.R.styleable#TabLayout_tabMaxWidth

ref com.google.android.material.R.styleable#TabLayout_tabTextAppearance

See also
<a href="http://www.google.com/design/spec/components/tabs.html">Tabs</a>

Summary

Nested types

This interface is deprecated.

Use OnTabSelectedListener instead.

Callback interface invoked when a tab's selection state changes.

public class TabLayout.Tab

A tab in this layout.

A ViewPager.OnPageChangeListener class which contains the necessary calls back to the provided TabLayout so that the tab position is kept in sync.

public final class TabLayout.TabView

A LinearLayout containing Tab instances for use with TabLayout.

A TabLayout.OnTabSelectedListener class which contains the necessary calls back to the provided ViewPager so that the tab position is kept in sync.

Constants

static final int

Gravity used to lay out the tabs in the center of the TabLayout.

static final int

Gravity used to fill the TabLayout as much as possible.

static final int

Gravity used to lay out the tabs aligned to the start of the TabLayout.

static final int

Indicator animation mode used to translate the selected tab indicator by growing and then shrinking the indicator, making the indicator look like it is stretching while translating between destinations.

static final int

Indicator animation mode used to switch the selected tab indicator from one tab to another by sequentially fading it out from the current destination and in at its new destination.

static final int

Indicator animation mode used to translate the selected tab indicator between two tabs using a linear motion.

static final int

Indicator gravity used to align the tab selection indicator to the bottom of the .

static final int

Indicator gravity used to align the tab selection indicator to the center of the .

static final int

Indicator gravity used to stretch the tab selection indicator across the entire height of the TabLayout.

static final int

Indicator gravity used to align the tab selection indicator to the top of the .

static final int

Auto-sizing tabs behave like MODE_FIXED with GRAVITY_CENTER while the tabs fit within the TabLayout's content width.

static final int

Fixed tabs display all tabs concurrently and are best used with content that benefits from quick pivots between tabs.

static final int

Scrollable tabs display a subset of tabs at any given moment, and can contain longer tab labels and a larger number of tabs.

static final int

This mode is set by default.

static final int

If a tab is instantiated with setText, and this mode is set, the text will be saved and utilized for the content description, but no visible labels will be created.

Public fields

int
ColorStateList
int
int
Drawable
ColorStateList

Public constructors

TabLayout(Context context)
TabLayout(Context context, AttributeSet attrs)
TabLayout(Context context, AttributeSet attrs, int defStyleAttr)

Public methods

void

This method is deprecated.

use addOnTabSelectedListener

void

Add a TabLayout.OnTabSelectedListener that will be invoked when tab selection changes.

void

Add a tab to this layout.

void
addTab(TabLayout.Tab tab, int position)

Add a tab to this layout.

void
addTab(TabLayout.Tab tab, boolean setSelected)

Add a tab to this layout.

void
addTab(TabLayout.Tab tab, int position, boolean setSelected)

Add a tab to this layout.

void
addView(View child)
void
addView(View child, int index)
void
addView(View child, LayoutParams params)
void
addView(View child, int index, LayoutParams params)
void

Remove all previously added TabLayout.OnTabSelectedListeners.

LayoutParams
generateLayoutParams(AttributeSet attrs)
int

Returns the position of the current selected tab.

TabLayout.Tab
getTabAt(int index)

Returns the tab at the specified index.

int

Returns the number of tabs currently registered with the tab layout.

int

The current gravity used for laying out tabs.

ColorStateList

Gets the icon tint for the different states (normal, selected) used for the tabs.

int

Get the current indicator animation mode used to animate the selection indicator between destinations.

int

Get the current indicator gravity used to align the tab selection indicator in the .

int

Returns the current mode used by this TabLayout.

ColorStateList

Returns the ripple color for this TabLayout.

Drawable

Returns the selection indicator drawable for this TabLayout.

ColorStateList

Gets the text colors for the different states (normal, selected) used for the tabs.

boolean

Returns whether this TabLayout has an unbounded ripple effect, or if ripple is bound to the tab item size.

boolean

Returns whether tab labels will be displayed inline with tab icons, or if they will be displayed underneath tab icons.

boolean

Get whether or not selection indicator width is fit to full width of the tab item, or fit to the tab item's content.

TabLayout.Tab

Create and return a new Tab.

void
onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
boolean
onInterceptTouchEvent(MotionEvent event)
boolean
onTouchEvent(MotionEvent event)
void

Remove all tabs from the tab layout and deselect the current tab.

void

This method is deprecated.

use removeOnTabSelectedListener

void

Remove the given TabLayout.OnTabSelectedListener that was previously added via addOnTabSelectedListener.

void

Remove a tab from the layout.

void
removeTabAt(int position)

Remove a tab from the layout.

void

Selects the given tab.

void
selectTab(TabLayout.Tab tab, boolean updateIndicator)

Selects the given tab.

void
setElevation(float elevation)
void
setInlineLabel(boolean inline)

Set whether tab labels will be displayed inline with tab icons, or if they will be displayed underneath tab icons.

void
setInlineLabelResource(int inlineResourceId)

Set whether tab labels will be displayed inline with tab icons, or if they will be displayed underneath tab icons.

void

This method is deprecated.

Use addOnTabSelectedListener and removeOnTabSelectedListener.

void

This method is deprecated.

Use addOnTabSelectedListener and removeOnTabSelectedListener.

void
setScrollPosition(
    int position,
    float positionOffset,
    boolean updateSelectedTabView
)

Set the scroll position of the TabLayout.

void
setScrollPosition(
    int position,
    float positionOffset,
    boolean updateSelectedTabView,
    boolean updateIndicatorPosition
)

Set the scroll position of the TabLayout.

void
setSelectedTabIndicator(Drawable tabSelectedIndicator)

Sets the selection indicator for this TabLayout.

void
setSelectedTabIndicator(int tabSelectedIndicatorResourceId)

Sets the drawable resource to use as the selection indicator for this TabLayout.

void

Sets the tab indicator's color for the currently selected tab.

void
setSelectedTabIndicatorGravity(int indicatorGravity)

Set the indicator gravity used to align the tab selection indicator in the TabLayout.

void

This method is deprecated.

If possible, set the intrinsic height directly on a custom indicator drawable passed to setSelectedTabIndicator.

void
setTabGravity(int gravity)

Set the gravity to use when laying out the tabs.

void
setTabIconTint(ColorStateList iconTint)

Sets the icon tint for the different states (normal, selected) used for the tabs.

void
setTabIconTintResource(int iconTintResourceId)

Sets the icon tint resource for the different states (normal, selected) used for the tabs.

void
setTabIndicatorAnimationMode(int tabIndicatorAnimationMode)

Set the mode by which the selection indicator should animate when moving between destinations.

void
setTabIndicatorFullWidth(boolean tabIndicatorFullWidth)

Enable or disable option to fit the tab selection indicator to the full width of the tab item rather than to the tab item's content.

void
setTabMode(int mode)

Set the behavior mode for the Tabs in this layout.

void
setTabRippleColor(ColorStateList color)

Sets the ripple color for this TabLayout.

void
setTabRippleColorResource(int tabRippleColorResourceId)

Sets the ripple color resource for this TabLayout.

void
setTabTextColors(ColorStateList textColor)

Sets the text colors for the different states (normal, selected) used for the tabs.

void
setTabTextColors(int normalColor, int selectedColor)

Sets the text colors for the different states (normal, selected) used for the tabs.

void
setTabsFromPagerAdapter(PagerAdapter adapter)

This method is deprecated.

Use setupWithViewPager to link a TabLayout with a ViewPager together.

void
setUnboundedRipple(boolean unboundedRipple)

Set whether this TabLayout will have an unbounded ripple effect or if ripple will be bound to the tab item size.

void
setUnboundedRippleResource(int unboundedRippleResourceId)

Set whether this TabLayout will have an unbounded ripple effect or if ripple will be bound to the tab item size.

void
setupWithViewPager(ViewPager viewPager)

The one-stop shop for setting up this TabLayout with a ViewPager.

void
setupWithViewPager(ViewPager viewPager, boolean autoRefresh)

The one-stop shop for setting up this TabLayout with a ViewPager.

boolean

Protected methods

TabLayout.Tab
void
void
void
onDraw(Canvas canvas)
void
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
boolean

Constants

GRAVITY_CENTER

public static final int GRAVITY_CENTER = 1

Gravity used to lay out the tabs in the center of the TabLayout.

GRAVITY_FILL

public static final int GRAVITY_FILL = 0

Gravity used to fill the TabLayout as much as possible. This option only takes effect when used with MODE_FIXED on non-landscape screens less than 600dp wide.

GRAVITY_START

public static final int GRAVITY_START = 2

Gravity used to lay out the tabs aligned to the start of the TabLayout.

INDICATOR_ANIMATION_MODE_ELASTIC

public static final int INDICATOR_ANIMATION_MODE_ELASTIC = 1

Indicator animation mode used to translate the selected tab indicator by growing and then shrinking the indicator, making the indicator look like it is stretching while translating between destinations.

The left and right side of the selection indicator translate out of step - with the right decelerating and the left accelerating (when moving right). This difference in velocity between the sides of the indicator, over the duration of the animation, make the indicator look like it grows and then shrinks back down to fit it's new destination's width.

ref com.google.android.material.R.styleable#TabLayout_tabIndicatorAnimationMode

INDICATOR_ANIMATION_MODE_FADE

public static final int INDICATOR_ANIMATION_MODE_FADE = 2

Indicator animation mode used to switch the selected tab indicator from one tab to another by sequentially fading it out from the current destination and in at its new destination.

ref com.google.android.material.R.styleable#TabLayout_tabIndicatorAnimationMode

INDICATOR_ANIMATION_MODE_LINEAR

public static final int INDICATOR_ANIMATION_MODE_LINEAR = 0

Indicator animation mode used to translate the selected tab indicator between two tabs using a linear motion.

The left and right side of the selection indicator translate in step over the duration of the animation. The only exception to this is when the indicator needs to change size to fit the width of its new destination tab's label.

ref com.google.android.material.R.styleable#TabLayout_tabIndicatorAnimationMode

INDICATOR_GRAVITY_BOTTOM

public static final int INDICATOR_GRAVITY_BOTTOM = 0

Indicator gravity used to align the tab selection indicator to the bottom of the . This will only take effect if the indicator height is set via the custom indicator drawable's intrinsic height (preferred), via the tabIndicatorHeight attribute (deprecated), or via setSelectedTabIndicatorHeight (deprecated). Otherwise, the indicator will not be shown. This is the default value.

ref com.google.android.material.R.styleable#TabLayout_tabIndicatorGravity

INDICATOR_GRAVITY_CENTER

public static final int INDICATOR_GRAVITY_CENTER = 1

Indicator gravity used to align the tab selection indicator to the center of the . This will only take effect if the indicator height is set via the custom indicator drawable's intrinsic height (preferred), via the tabIndicatorHeight attribute (deprecated), or via setSelectedTabIndicatorHeight (deprecated). Otherwise, the indicator will not be shown.

ref com.google.android.material.R.styleable#TabLayout_tabIndicatorGravity

INDICATOR_GRAVITY_STRETCH

public static final int INDICATOR_GRAVITY_STRETCH = 3

Indicator gravity used to stretch the tab selection indicator across the entire height of the TabLayout. This will disregard tabIndicatorHeight and the indicator drawable's intrinsic height, if set.

ref com.google.android.material.R.styleable#TabLayout_tabIndicatorGravity

INDICATOR_GRAVITY_TOP

public static final int INDICATOR_GRAVITY_TOP = 2

Indicator gravity used to align the tab selection indicator to the top of the . This will only take effect if the indicator height is set via the custom indicator drawable's intrinsic height (preferred), via the tabIndicatorHeight attribute (deprecated), or via setSelectedTabIndicatorHeight (deprecated). Otherwise, the indicator will not be shown.

ref com.google.android.material.R.styleable#TabLayout_tabIndicatorGravity

MODE_AUTO

public static final int MODE_AUTO = 2

Auto-sizing tabs behave like MODE_FIXED with GRAVITY_CENTER while the tabs fit within the TabLayout's content width. Fixed tabs have equal width, based on the widest tab label. Once the tabs outgrow the view's width, auto-sizing tabs behave like MODE_SCROLLABLE, allowing for a dynamic number of tabs without requiring additional layout logic.

MODE_FIXED

public static final int MODE_FIXED = 1

Fixed tabs display all tabs concurrently and are best used with content that benefits from quick pivots between tabs. The maximum number of tabs is limited by the view's width. Fixed tabs have equal width, based on the widest tab label.

MODE_SCROLLABLE

public static final int MODE_SCROLLABLE = 0

Scrollable tabs display a subset of tabs at any given moment, and can contain longer tab labels and a larger number of tabs. They are best used for browsing contexts in touch interfaces when users don't need to directly compare the tab labels.

TAB_LABEL_VISIBILITY_LABELED

public static final int TAB_LABEL_VISIBILITY_LABELED = 1

This mode is set by default. If a tab is instantiated with setText, a visible label will be created.

TAB_LABEL_VISIBILITY_UNLABELED

public static final int TAB_LABEL_VISIBILITY_UNLABELED = 0

If a tab is instantiated with setText, and this mode is set, the text will be saved and utilized for the content description, but no visible labels will be created.

Public fields

tabGravity

public int tabGravity

tabIconTint

public ColorStateList tabIconTint

tabIndicatorAnimationMode

public int tabIndicatorAnimationMode

tabIndicatorGravity

public int tabIndicatorGravity

tabSelectedIndicator

public Drawable tabSelectedIndicator

tabTextColors

public ColorStateList tabTextColors

Public constructors

TabLayout

public TabLayout(Context context)

TabLayout

public TabLayout(Context context, AttributeSet attrs)

TabLayout

public TabLayout(Context context, AttributeSet attrs, int defStyleAttr)

Public methods

addOnTabSelectedListener

public void addOnTabSelectedListener(TabLayout.BaseOnTabSelectedListener listener)

Add a TabLayout.BaseOnTabSelectedListener that will be invoked when tab selection changes.

Components that add a listener should take care to remove it when finished via removeOnTabSelectedListener.

Parameters
TabLayout.BaseOnTabSelectedListener listener

listener to add

addOnTabSelectedListener

public void addOnTabSelectedListener(TabLayout.OnTabSelectedListener listener)

Add a TabLayout.OnTabSelectedListener that will be invoked when tab selection changes.

Components that add a listener should take care to remove it when finished via removeOnTabSelectedListener.

Parameters
TabLayout.OnTabSelectedListener listener

listener to add

addTab

public void addTab(TabLayout.Tab tab)

Add a tab to this layout. The tab will be added at the end of the list. If this is the first tab to be added it will become the selected tab.

Parameters
TabLayout.Tab tab

Tab to add

addTab

public void addTab(TabLayout.Tab tab, int position)

Add a tab to this layout. The tab will be inserted at position. If this is the first tab to be added it will become the selected tab.

Parameters
TabLayout.Tab tab

The tab to add

int position

The new position of the tab

addTab

public void addTab(TabLayout.Tab tab, boolean setSelected)

Add a tab to this layout. The tab will be added at the end of the list.

Parameters
TabLayout.Tab tab

Tab to add

boolean setSelected

True if the added tab should become the selected tab.

addTab

public void addTab(TabLayout.Tab tab, int position, boolean setSelected)

Add a tab to this layout. The tab will be inserted at position.

Parameters
TabLayout.Tab tab

The tab to add

int position

The new position of the tab

boolean setSelected

True if the added tab should become the selected tab.

addView

public void addView(View child)

addView

public void addView(View child, int index)

addView

public void addView(View child, LayoutParams params)

addView

public void addView(View child, int index, LayoutParams params)

clearOnTabSelectedListeners

public void clearOnTabSelectedListeners()

Remove all previously added TabLayout.OnTabSelectedListeners.

generateLayoutParams

public LayoutParams generateLayoutParams(AttributeSet attrs)

getSelectedTabPosition

public int getSelectedTabPosition()

Returns the position of the current selected tab.

Returns
int

selected tab position, or -1 if there isn't a selected tab.

getTabAt

public TabLayout.Tab getTabAt(int index)

Returns the tab at the specified index.

getTabCount

public int getTabCount()

Returns the number of tabs currently registered with the tab layout.

Returns
int

Tab count

getTabGravity

public int getTabGravity()

The current gravity used for laying out tabs.

Returns
int

one of GRAVITY_CENTER or GRAVITY_FILL.

getTabIconTint

public ColorStateList getTabIconTint()

Gets the icon tint for the different states (normal, selected) used for the tabs.

getTabIndicatorAnimationMode

public int getTabIndicatorAnimationMode()

Get the current indicator animation mode used to animate the selection indicator between destinations.

ref com.google.android.material.R.styleable#TabLayout_tabIndicatorAnimationMode

getTabIndicatorGravity

public int getTabIndicatorGravity()

Get the current indicator gravity used to align the tab selection indicator in the .

getTabMode

public int getTabMode()

Returns the current mode used by this TabLayout.

See also
setTabMode

getTabRippleColor

public ColorStateList getTabRippleColor()

Returns the ripple color for this TabLayout.

Returns
ColorStateList

the color (or ColorStateList) used for the ripple

getTabSelectedIndicator

public Drawable getTabSelectedIndicator()

Returns the selection indicator drawable for this TabLayout.

Returns
Drawable

The drawable used as the tab selection indicator, if set.

getTabTextColors

public ColorStateList getTabTextColors()

Gets the text colors for the different states (normal, selected) used for the tabs.

hasUnboundedRipple

public boolean hasUnboundedRipple()

Returns whether this TabLayout has an unbounded ripple effect, or if ripple is bound to the tab item size.

ref com.google.android.material.R.styleable#TabLayout_tabUnboundedRipple

isInlineLabel

public boolean isInlineLabel()

Returns whether tab labels will be displayed inline with tab icons, or if they will be displayed underneath tab icons.

ref com.google.android.material.R.styleable#TabLayout_tabInlineLabel

See also
setInlineLabel

isTabIndicatorFullWidth

public boolean isTabIndicatorFullWidth()

Get whether or not selection indicator width is fit to full width of the tab item, or fit to the tab item's content.

ref com.google.android.material.R.styleable#TabLayout_tabIndicatorFullWidth

Returns
boolean

whether or not selection indicator width is fit to the full width of the tab item

newTab

public TabLayout.Tab newTab()

Create and return a new Tab. You need to manually add this using addTab or a related method.

Returns
TabLayout.Tab

A new Tab

See also
addTab

onInitializeAccessibilityNodeInfo

public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)

onInterceptTouchEvent

public boolean onInterceptTouchEvent(MotionEvent event)

onTouchEvent

public boolean onTouchEvent(MotionEvent event)

removeAllTabs

public void removeAllTabs()

Remove all tabs from the tab layout and deselect the current tab.

removeOnTabSelectedListener

public void removeOnTabSelectedListener(
    TabLayout.BaseOnTabSelectedListener listener
)

Remove the given TabLayout.BaseOnTabSelectedListener that was previously added via addOnTabSelectedListener.

Parameters
TabLayout.BaseOnTabSelectedListener listener

listener to remove

removeOnTabSelectedListener

public void removeOnTabSelectedListener(TabLayout.OnTabSelectedListener listener)

Remove the given TabLayout.OnTabSelectedListener that was previously added via addOnTabSelectedListener.

Parameters
TabLayout.OnTabSelectedListener listener

listener to remove

removeTab

public void removeTab(TabLayout.Tab tab)

Remove a tab from the layout. If the removed tab was selected it will be deselected and another tab will be selected if present.

Parameters
TabLayout.Tab tab

The tab to remove

removeTabAt

public void removeTabAt(int position)

Remove a tab from the layout. If the removed tab was selected it will be deselected and another tab will be selected if present.

Parameters
int position

Position of the tab to remove

selectTab

public void selectTab(TabLayout.Tab tab)

Selects the given tab.

Parameters
TabLayout.Tab tab

The tab to select, or null to select none.

See also
selectTab

selectTab

public void selectTab(TabLayout.Tab tab, boolean updateIndicator)

Selects the given tab. Will always animate to the selected tab if the current tab is reselected, regardless of the value of updateIndicator.

Parameters
TabLayout.Tab tab

The tab to select, or null to select none.

boolean updateIndicator

Whether to update the indicator.

See also
selectTab

setElevation

public void setElevation(float elevation)

setInlineLabel

public void setInlineLabel(boolean inline)

Set whether tab labels will be displayed inline with tab icons, or if they will be displayed underneath tab icons.

ref com.google.android.material.R.styleable#TabLayout_tabInlineLabel

See also
isInlineLabel

setInlineLabelResource

public void setInlineLabelResource(int inlineResourceId)

Set whether tab labels will be displayed inline with tab icons, or if they will be displayed underneath tab icons.

ref com.google.android.material.R.styleable#TabLayout_tabInlineLabel

Parameters
int inlineResourceId

Resource ID for boolean inline flag

See also
isInlineLabel

setOnTabSelectedListener

public void setOnTabSelectedListener(TabLayout.BaseOnTabSelectedListener listener)

setOnTabSelectedListener

public void setOnTabSelectedListener(TabLayout.OnTabSelectedListener listener)

setScrollPosition

public void setScrollPosition(
    int position,
    float positionOffset,
    boolean updateSelectedTabView
)

Set the scroll position of the TabLayout.

Parameters
int position

Position of the tab to scroll.

float positionOffset

Value from [0, 1) indicating the offset from position.

boolean updateSelectedTabView

Whether to draw the tab at the specified position + positionOffset as selected.

Note that calling the method with updateSelectedTabView = truedoes not select a tab at the specified position, but only draws it as selected. This can be useful for when the TabLayout behavior needs to be linked to another view, such as androidx.viewpager.widget.ViewPager.

setScrollPosition

public void setScrollPosition(
    int position,
    float positionOffset,
    boolean updateSelectedTabView,
    boolean updateIndicatorPosition
)

Set the scroll position of the TabLayout.

Parameters
int position

Position of the tab to scroll.

float positionOffset

Value from [0, 1) indicating the offset from position.

boolean updateSelectedTabView

Whether to draw the tab at the specified position + positionOffset as selected.

Note that calling the method with updateSelectedTabView = truedoes not select a tab at the specified position, but only draws it as selected. This can be useful for when the TabLayout behavior needs to be linked to another view, such as androidx.viewpager.widget.ViewPager.

boolean updateIndicatorPosition

Whether to set the indicator to the specified position and offset.

Note that calling the method with updateIndicatorPosition = truedoes not select a tab at the specified position, but only updates the indicator position. This can be useful for when the TabLayout behavior needs to be linked to another view, such as androidx.viewpager.widget.ViewPager.

setSelectedTabIndicator

public void setSelectedTabIndicator(Drawable tabSelectedIndicator)

Sets the selection indicator for this TabLayout. By default, this is a line along the bottom of the tab. If tabIndicatorColor is specified via the TabLayout's style or via setSelectedTabIndicatorColor the selection indicator will be tinted that color. Otherwise, it will use the colors specified in the drawable.

Setting the indicator drawable to null will cause TabLayout to use the default, GradientDrawable line indicator.

Parameters
Drawable tabSelectedIndicator

A drawable to use as the selected tab indicator.

setSelectedTabIndicator

public void setSelectedTabIndicator(int tabSelectedIndicatorResourceId)

Sets the drawable resource to use as the selection indicator for this TabLayout. By default, this is a line along the bottom of the tab. If tabIndicatorColor is specified via the TabLayout's style or via setSelectedTabIndicatorColor the selection indicator will be tinted that color. Otherwise, it will use the colors specified in the drawable.

Parameters
int tabSelectedIndicatorResourceId

A drawable resource to use as the selected tab indicator.

setSelectedTabIndicatorColor

public void setSelectedTabIndicatorColor(int color)

Sets the tab indicator's color for the currently selected tab.

If the tab indicator color is not Color.TRANSPARENT, the indicator will be wrapped and tinted right before it is drawn by draw. If you'd like the inherent color or the tinted color of a custom drawable to be used, make sure this color is set to Color.TRANSPARENT to avoid your color/tint being overridden.

ref com.google.android.material.R.styleable#TabLayout_tabIndicatorColor

Parameters
int color

color to use for the indicator

setSelectedTabIndicatorGravity

public void setSelectedTabIndicatorGravity(int indicatorGravity)

Set the indicator gravity used to align the tab selection indicator in the TabLayout. You must set the indicator height via the custom indicator drawable's intrinsic height (preferred), via the tabIndicatorHeight attribute (deprecated), or via setSelectedTabIndicatorHeight (deprecated). Otherwise, the indicator will not be shown unless gravity is set to INDICATOR_GRAVITY_STRETCH, in which case it will ignore indicator height and stretch across the entire height of the TabLayout. This defaults to INDICATOR_GRAVITY_BOTTOM if not set.

ref com.google.android.material.R.styleable#TabLayout_tabIndicatorGravity

setSelectedTabIndicatorHeight

public void setSelectedTabIndicatorHeight(int height)

Sets the tab indicator's height for the currently selected tab.

ref com.google.android.material.R.styleable#TabLayout_tabIndicatorHeight

Parameters
int height

height to use for the indicator in pixels

setTabGravity

public void setTabGravity(int gravity)

Set the gravity to use when laying out the tabs.

ref com.google.android.material.R.styleable#TabLayout_tabGravity

Parameters
int gravity

one of GRAVITY_CENTER or GRAVITY_FILL.

setTabIconTint

public void setTabIconTint(ColorStateList iconTint)

Sets the icon tint for the different states (normal, selected) used for the tabs.

See also
getTabIconTint

setTabIconTintResource

public void setTabIconTintResource(int iconTintResourceId)

Sets the icon tint resource for the different states (normal, selected) used for the tabs.

Parameters
int iconTintResourceId

A color resource to use as icon tint.

See also
getTabIconTint

setTabIndicatorAnimationMode

public void setTabIndicatorAnimationMode(int tabIndicatorAnimationMode)

Set the mode by which the selection indicator should animate when moving between destinations.

Defaults to INDICATOR_ANIMATION_MODE_LINEAR. Changing this is useful as a stylistic choice.

ref com.google.android.material.R.styleable#TabLayout_tabIndicatorAnimationMode

Parameters
int tabIndicatorAnimationMode

one of INDICATOR_ANIMATION_MODE_LINEAR or INDICATOR_ANIMATION_MODE_ELASTIC

setTabIndicatorFullWidth

public void setTabIndicatorFullWidth(boolean tabIndicatorFullWidth)

Enable or disable option to fit the tab selection indicator to the full width of the tab item rather than to the tab item's content.

Defaults to true. If set to false and the tab item has a text label, the selection indicator width will be set to the width of the text label. If the tab item has no text label, but does have an icon, the selection indicator width will be set to the icon. If the tab item has neither of these, or if the calculated width is less than a minimum width value, the selection indicator width will be set to the minimum width value.

ref com.google.android.material.R.styleable#TabLayout_tabIndicatorFullWidth

Parameters
boolean tabIndicatorFullWidth

Whether or not to fit selection indicator width to full width of the tab item

setTabMode

public void setTabMode(int mode)

Set the behavior mode for the Tabs in this layout. The valid input options are:

  • MODE_FIXED: Fixed tabs display all tabs concurrently and are best used with content that benefits from quick pivots between tabs.
  • MODE_SCROLLABLE: Scrollable tabs display a subset of tabs at any given moment, and can contain longer tab labels and a larger number of tabs. They are best used for browsing contexts in touch interfaces when users don't need to directly compare the tab labels. This mode is commonly used with a androidx.viewpager.widget.ViewPager.

ref com.google.android.material.R.styleable#TabLayout_tabMode

Parameters
int mode

one of MODE_FIXED or MODE_SCROLLABLE.

setTabRippleColor

public void setTabRippleColor(ColorStateList color)

Sets the ripple color for this TabLayout.

When running on devices with KitKat, we draw this color as a filled overlay rather than a ripple.

ref com.google.android.material.R.styleable#TabLayout_tabRippleColor

Parameters
ColorStateList color

color (or ColorStateList) to use for the ripple

setTabRippleColorResource

public void setTabRippleColorResource(int tabRippleColorResourceId)

Sets the ripple color resource for this TabLayout.

When running on devices with KitKat, we draw this color as a filled overlay rather than a ripple.

Parameters
int tabRippleColorResourceId

A color resource to use as ripple color.

setTabTextColors

public void setTabTextColors(ColorStateList textColor)

Sets the text colors for the different states (normal, selected) used for the tabs.

See also
getTabTextColors

setTabTextColors

public void setTabTextColors(int normalColor, int selectedColor)

Sets the text colors for the different states (normal, selected) used for the tabs.

ref com.google.android.material.R.styleable#TabLayout_tabTextColor

ref com.google.android.material.R.styleable#TabLayout_tabSelectedTextColor

setTabsFromPagerAdapter

public void setTabsFromPagerAdapter(PagerAdapter adapter)

setUnboundedRipple

public void setUnboundedRipple(boolean unboundedRipple)

Set whether this TabLayout will have an unbounded ripple effect or if ripple will be bound to the tab item size.

Defaults to false.

ref com.google.android.material.R.styleable#TabLayout_tabUnboundedRipple

setUnboundedRippleResource

public void setUnboundedRippleResource(int unboundedRippleResourceId)

Set whether this TabLayout will have an unbounded ripple effect or if ripple will be bound to the tab item size. Defaults to false.

ref com.google.android.material.R.styleable#TabLayout_tabUnboundedRipple

Parameters
int unboundedRippleResourceId

Resource ID for boolean unbounded ripple value

setupWithViewPager

public void setupWithViewPager(ViewPager viewPager)

The one-stop shop for setting up this TabLayout with a ViewPager.

This is the same as calling setupWithViewPager with auto-refresh enabled.

Parameters
ViewPager viewPager

the ViewPager to link to, or null to clear any previous link

setupWithViewPager

public void setupWithViewPager(ViewPager viewPager, boolean autoRefresh)

The one-stop shop for setting up this TabLayout with a ViewPager.

This method will link the given ViewPager and this TabLayout together so that changes in one are automatically reflected in the other. This includes scroll state changes and clicks. The tabs displayed in this layout will be populated from the ViewPager adapter's page titles.

If autoRefresh is true, any changes in the PagerAdapter will trigger this layout to re-populate itself from the adapter's titles.

If the given ViewPager is non-null, it needs to already have a PagerAdapter set.

Parameters
ViewPager viewPager

the ViewPager to link to, or null to clear any previous link

boolean autoRefresh

whether this layout should refresh its contents if the given ViewPager's content changes

shouldDelayChildPressedState

public boolean shouldDelayChildPressedState()

Protected methods

createTabFromPool

protected TabLayout.Tab createTabFromPool()

onAttachedToWindow

protected void onAttachedToWindow()

onDetachedFromWindow

protected void onDetachedFromWindow()

onDraw

protected void onDraw(Canvas canvas)

onMeasure

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

releaseFromTabPool

protected boolean releaseFromTabPool(TabLayout.Tab tab)