Android APIs
public class

TabLayout

extends HorizontalScrollView
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ android.widget.HorizontalScrollView
           ↳ android.support.design.widget.TabLayout

Class Overview

TabLayout provides a horizontal layout to display tabs.

Population of the tabs to display is done through TabLayout.Tab instances. You create tabs via newTab(). From there you can change the tab's label or icon via setText(int) and setIcon(int) respectively. To display the tab, you need to add it to the layout via one of the addTab(Tab) 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 set a listener via setOnTabSelectedListener(OnTabSelectedListener) to be notified when any tab's selection state has been changed.

If you're using a ViewPager together with this layout, you can use setTabsFromPagerAdapter(PagerAdapter) which will populate the tabs using the given PagerAdapter's page titles. You should also use a TabLayout.TabLayoutOnPageChangeListener to forward the scroll and selection changes to this layout like so:

 ViewPager viewPager = ...;
 TabLayout tabLayout = ...;
 viewPager.addOnPageChangeListener(new TabLayoutOnPageChangeListener(tabLayout));
 

See Also

Summary

Nested Classes
interface TabLayout.OnTabSelectedListener Callback interface invoked when a tab's selection state changes. 
class TabLayout.Tab A tab in this layout. 
class TabLayout.TabLayoutOnPageChangeListener A ViewPager.OnPageChangeListener class which contains the necessary calls back to the provided TabLayout so that the tab position is kept in sync. 
class TabLayout.ViewPagerOnTabSelectedListener A TabLayout.OnTabSelectedListener class which contains the necessary calls back to the provided ViewPager so that the tab position is kept in sync. 
[Expand]
Inherited XML Attributes
From class android.widget.HorizontalScrollView
From class android.widget.FrameLayout
From class android.view.ViewGroup
From class android.view.View
Constants
int GRAVITY_CENTER Gravity used to lay out the tabs in the center of the TabLayout.
int GRAVITY_FILL Gravity used to fill the TabLayout as much as possible.
int MODE_FIXED Fixed tabs display all tabs concurrently and are best used with content that benefits from quick pivots between tabs.
int 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.
[Expand]
Inherited Constants
From class android.view.ViewGroup
From class android.view.View
[Expand]
Inherited Fields
From class android.view.View
Public Constructors
TabLayout(Context context)
TabLayout(Context context, AttributeSet attrs)
TabLayout(Context context, AttributeSet attrs, int defStyleAttr)
Public Methods
void addTab(TabLayout.Tab tab, boolean setSelected)
Add a tab to this layout.
void addTab(TabLayout.Tab tab, int position)
Add a tab to this layout.
void addTab(TabLayout.Tab tab, int position, boolean setSelected)
Add a tab to this layout.
void addTab(TabLayout.Tab tab)
Add a tab to this layout.
int getSelectedTabPosition()
Returns the position of the current selected tab.
TabLayout.Tab getTabAt(int index)
Returns the tab at the specified index.
int getTabCount()
Returns the number of tabs currently registered with the action bar.
int getTabGravity()
The current gravity used for laying out tabs.
int getTabMode()
Returns the current mode used by this TabLayout.
ColorStateList getTabTextColors()
Gets the text colors for the different states (normal, selected) used for the tabs.
TabLayout.Tab newTab()
Create and return a new TabLayout.Tab.
void removeAllTabs()
Remove all tabs from the action bar and deselect the current tab.
void removeTab(TabLayout.Tab tab)
Remove a tab from the layout.
void removeTabAt(int position)
Remove a tab from the layout.
void setOnTabSelectedListener(TabLayout.OnTabSelectedListener onTabSelectedListener)
Set the TabLayout.OnTabSelectedListener that will handle switching to and from tabs.
void setScrollPosition(int position, float positionOffset, boolean updateSelectedText)
Set the scroll position of the tabs.
void setSelectedTabIndicatorColor(int color)
Sets the tab indicator's color for the currently selected tab.
void setSelectedTabIndicatorHeight(int height)
Sets the tab indicator's height for the currently selected tab.
void setTabGravity(int gravity)
Set the gravity to use when laying out the tabs.
void setTabMode(int mode)
Set the behavior mode for the Tabs in this layout.
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)
Populate our tab content from the given PagerAdapter.
void setupWithViewPager(ViewPager viewPager)
The one-stop shop for setting up this TabLayout with a ViewPager.
Protected Methods
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.

[Expand]
Inherited Methods
From class android.widget.HorizontalScrollView
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

Constants

public static final int GRAVITY_CENTER

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

Constant Value: 1 (0x00000001)

public static final int GRAVITY_FILL

Gravity used to fill the TabLayout as much as possible. This option only takes effect when used with MODE_FIXED.

Constant Value: 0 (0x00000000)

public static final int MODE_FIXED

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.

Constant Value: 1 (0x00000001)

public static final int 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.

Constant Value: 0 (0x00000000)

Public Constructors

public TabLayout (Context context)

public TabLayout (Context context, AttributeSet attrs)

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

Public Methods

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
tab Tab to add
setSelected True if the added tab should become the selected tab.

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
tab The tab to add
position The new position of the tab

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

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

Parameters
tab The tab to add
position The new position of the tab
setSelected True if the added tab should become the selected tab.

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
tab Tab to add

public int getSelectedTabPosition ()

Returns the position of the current selected tab.

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

public TabLayout.Tab getTabAt (int index)

Returns the tab at the specified index.

public int getTabCount ()

Returns the number of tabs currently registered with the action bar.

Returns
  • Tab count

public int getTabGravity ()

The current gravity used for laying out tabs.

Returns

public int getTabMode ()

Returns the current mode used by this TabLayout.

See Also

public ColorStateList getTabTextColors ()

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

public TabLayout.Tab newTab ()

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

Returns
  • A new Tab
See Also

public void removeAllTabs ()

Remove all tabs from the action bar and deselect the current tab.

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
tab The tab to remove

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
position Position of the tab to remove

public void setOnTabSelectedListener (TabLayout.OnTabSelectedListener onTabSelectedListener)

Set the TabLayout.OnTabSelectedListener that will handle switching to and from tabs.

Parameters
onTabSelectedListener Listener to handle tab selection events

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

Set the scroll position of the tabs. This is useful for when the tabs are being displayed as part of a scrolling container such as ViewPager.

Calling this method does not update the selected tab, it is only used for drawing purposes.

Parameters
position current scroll position
positionOffset Value from [0, 1) indicating the offset from position.
updateSelectedText Whether to update the text's selected state.

public void setSelectedTabIndicatorColor (int color)

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

Parameters
color color to use for the indicator

public void setSelectedTabIndicatorHeight (int height)

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

Parameters
height height to use for the indicator in pixels

public void setTabGravity (int gravity)

Set the gravity to use when laying out the tabs.

Parameters
gravity one of GRAVITY_CENTER or GRAVITY_FILL.

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 ViewPager.

Parameters
mode one of MODE_FIXED or MODE_SCROLLABLE.

public void setTabTextColors (ColorStateList textColor)

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

public void setTabTextColors (int normalColor, int selectedColor)

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

public void setTabsFromPagerAdapter (PagerAdapter adapter)

Populate our tab content from the given PagerAdapter.

Any existing tabs will be removed first. Each tab will have it's text set to the value returned from getPageTitle(int)

Parameters
adapter the adapter to populate from

public void setupWithViewPager (ViewPager viewPager)

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

This method will:

Protected Methods

protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overridden by subclasses to provide accurate and efficient measurement of their contents.

CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int, int). Calling the superclass' onMeasure(int, int) is a valid use.

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int, int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

Parameters
widthMeasureSpec horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.