TabLayout
public
class
TabLayout
extends HorizontalScrollView
java.lang.Object | |||||
↳ | android.view.View | ||||
↳ | android.view.ViewGroup | ||||
↳ | android.widget.FrameLayout | ||||
↳ | android.widget.HorizontalScrollView | ||||
↳ | com.google.android.material.tabs.TabLayout |
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 TabLayout.Tab.setText(int)
and TabLayout.Tab.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 add a listener via
addOnTabSelectedListener(OnTabSelectedListener)
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 ViewPager
together with this layout, you
can call setupWithViewPager(ViewPager)
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.
See also:
Summary
Nested classes | |
---|---|
interface |
TabLayout.BaseOnTabSelectedListener<T extends TabLayout.Tab>
This interface is deprecated.
Use |
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 |
class |
TabLayout.TabView
A |
class |
TabLayout.ViewPagerOnTabSelectedListener
A |
XML attributes | |
---|---|
TabLayout_tabBackground |
|
TabLayout_tabContentStart |
|
TabLayout_tabGravity |
|
TabLayout_tabIndicatorAnimationMode |
|
TabLayout_tabIndicatorColor |
|
TabLayout_tabIndicatorFullWidth |
|
TabLayout_tabIndicatorGravity |
|
TabLayout_tabIndicatorHeight |
|
TabLayout_tabInlineLabel |
|
TabLayout_tabMaxWidth |
|
TabLayout_tabMinWidth |
|
TabLayout_tabMode |
|
TabLayout_tabPadding |
|
TabLayout_tabPaddingBottom |
|
TabLayout_tabPaddingEnd |
|
TabLayout_tabPaddingStart |
|
TabLayout_tabPaddingTop |
|
TabLayout_tabRippleColor |
|
TabLayout_tabSelectedTextColor |
|
TabLayout_tabTextAppearance |
|
TabLayout_tabTextColor |
|
TabLayout_tabUnboundedRipple |
Constants | |
---|---|
int |
GRAVITY_CENTER
Gravity used to lay out the tabs in the center of the |
int |
GRAVITY_FILL
Gravity used to fill the |
int |
GRAVITY_START
Gravity used to lay out the tabs aligned to the start of the |
int |
INDICATOR_ANIMATION_MODE_ELASTIC
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. |
int |
INDICATOR_ANIMATION_MODE_FADE
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. |
int |
INDICATOR_ANIMATION_MODE_LINEAR
Indicator animation mode used to translate the selected tab indicator between two tabs using a linear motion. |
int |
INDICATOR_GRAVITY_BOTTOM
Indicator gravity used to align the tab selection indicator to the bottom of the |
int |
INDICATOR_GRAVITY_CENTER
Indicator gravity used to align the tab selection indicator to the center of the |
int |
INDICATOR_GRAVITY_STRETCH
Indicator gravity used to stretch the tab selection indicator across the entire height
of the |
int |
INDICATOR_GRAVITY_TOP
Indicator gravity used to align the tab selection indicator to the top of the |
int |
MODE_AUTO
Auto-sizing tabs behave like MODE_FIXED with GRAVITY_CENTER while the tabs fit within the TabLayout's content width. |
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. |
int |
TAB_LABEL_VISIBILITY_LABELED
This mode is set by default. |
int |
TAB_LABEL_VISIBILITY_UNLABELED
If a tab is instantiated with |
Inherited constants |
---|
Inherited fields |
---|
Public constructors | |
---|---|
TabLayout(Context context)
|
|
TabLayout(Context context, AttributeSet attrs)
|
|
TabLayout(Context context, AttributeSet attrs, int defStyleAttr)
|
Public methods | |
---|---|
void
|
addOnTabSelectedListener(TabLayout.OnTabSelectedListener listener)
Add a |
void
|
addOnTabSelectedListener(BaseOnTabSelectedListener listener)
This method is deprecated.
use |
void
|
addTab(TabLayout.Tab tab)
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)
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, int index)
|
void
|
addView(View child, ViewGroup.LayoutParams params)
|
void
|
addView(View child, int index, ViewGroup.LayoutParams params)
|
void
|
addView(View child)
|
void
|
clearOnTabSelectedListeners()
Remove all previously added |
FrameLayout.LayoutParams
|
generateLayoutParams(AttributeSet attrs)
|
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 tab layout. |
int
|
getTabGravity()
The current gravity used for laying out tabs. |
ColorStateList
|
getTabIconTint()
Gets the icon tint for the different states (normal, selected) used for the tabs. |
int
|
getTabIndicatorAnimationMode()
Get the current indicator animation mode used to animate the selection indicator between destinations. |
int
|
getTabIndicatorGravity()
Get the current indicator gravity used to align the tab selection indicator in the |
int
|
getTabMode()
Returns the current mode used by this |
ColorStateList
|
getTabRippleColor()
Returns the ripple color for this TabLayout. |
Drawable
|
getTabSelectedIndicator()
Returns the selection indicator drawable for this TabLayout. |
ColorStateList
|
getTabTextColors()
Gets the text colors for the different states (normal, selected) used for the tabs. |
boolean
|
hasUnboundedRipple()
Returns whether this |
boolean
|
isInlineLabel()
Returns whether tab labels will be displayed inline with tab icons, or if they will be displayed underneath tab icons. |
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. |
TabLayout.Tab
|
newTab()
Create and return a new |
void
|
onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
|
boolean
|
onInterceptTouchEvent(MotionEvent event)
|
boolean
|
onTouchEvent(MotionEvent event)
|
void
|
removeAllTabs()
Remove all tabs from the tab layout and deselect the current tab. |
void
|
removeOnTabSelectedListener(TabLayout.OnTabSelectedListener listener)
Remove the given |
void
|
removeOnTabSelectedListener(BaseOnTabSelectedListener listener)
This method is deprecated.
use |
void
|
removeTab(TabLayout.Tab tab)
Remove a tab from the layout. |
void
|
removeTabAt(int position)
Remove a tab from the layout. |
void
|
selectTab(TabLayout.Tab tab)
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
|
setOnTabSelectedListener(BaseOnTabSelectedListener listener)
This method is deprecated.
Use |
void
|
setOnTabSelectedListener(TabLayout.OnTabSelectedListener listener)
This method is deprecated.
Use |
void
|
setScrollPosition(int position, float positionOffset, boolean updateSelectedTabView, boolean updateIndicatorPosition)
Set the scroll position of the |
void
|
setScrollPosition(int position, float positionOffset, boolean updateSelectedTabView)
Set the scroll position of the |
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
|
setSelectedTabIndicatorColor(int color)
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 |
void
|
setSelectedTabIndicatorHeight(int height)
This method is deprecated.
If possible, set the intrinsic height directly on a custom indicator drawable
passed to |
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(int normalColor, int selectedColor)
Sets the text colors for the different states (normal, selected) used for the tabs. |
void
|
setTabTextColors(ColorStateList textColor)
Sets the text colors for the different states (normal, selected) used for the tabs. |
void
|
setTabsFromPagerAdapter(PagerAdapter adapter)
This method is deprecated.
Use |
void
|
setUnboundedRipple(boolean unboundedRipple)
Set whether this |
void
|
setUnboundedRippleResource(int unboundedRippleResourceId)
Set whether this |
void
|
setupWithViewPager(ViewPager viewPager)
The one-stop shop for setting up this |
void
|
setupWithViewPager(ViewPager viewPager, boolean autoRefresh)
The one-stop shop for setting up this |
boolean
|
shouldDelayChildPressedState()
|
Protected methods | |
---|---|
TabLayout.Tab
|
createTabFromPool()
|
void
|
onAttachedToWindow()
|
void
|
onDetachedFromWindow()
|
void
|
onDraw(Canvas canvas)
|
void
|
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
|
boolean
|
releaseFromTabPool(TabLayout.Tab tab)
|
Inherited methods | |
---|---|
XML attributes
TabLayout_tabBackground
TabLayout_tabContentStart
TabLayout_tabGravity
Related methods:
TabLayout_tabIndicatorAnimationMode
Related methods:
TabLayout_tabIndicatorColor
Related methods:
TabLayout_tabIndicatorFullWidth
Related methods:
TabLayout_tabIndicatorGravity
Related methods:
TabLayout_tabIndicatorHeight
Related methods:
TabLayout_tabInlineLabel
Related methods:
TabLayout_tabMaxWidth
TabLayout_tabMinWidth
TabLayout_tabMode
Related methods:
TabLayout_tabPadding
TabLayout_tabPaddingBottom
TabLayout_tabPaddingEnd
TabLayout_tabPaddingStart
TabLayout_tabPaddingTop
TabLayout_tabRippleColor
Related methods:
TabLayout_tabSelectedTextColor
Related methods:
TabLayout_tabTextAppearance
TabLayout_tabTextColor
Related methods:
TabLayout_tabUnboundedRipple
Related methods:
Constants
GRAVITY_CENTER
public static final int GRAVITY_CENTER
Gravity used to lay out the tabs in the center of the TabLayout
.
See also:
Constant Value: 1 (0x00000001)
GRAVITY_FILL
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
on non-landscape screens less than 600dp wide.
See also:
Constant Value: 0 (0x00000000)
GRAVITY_START
public static final int GRAVITY_START
Gravity used to lay out the tabs aligned to the start of the TabLayout
.
See also:
Constant Value: 2 (0x00000002)
INDICATOR_ANIMATION_MODE_ELASTIC
public static final int INDICATOR_ANIMATION_MODE_ELASTIC
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.
Constant Value: 1 (0x00000001)
INDICATOR_ANIMATION_MODE_FADE
public static final int INDICATOR_ANIMATION_MODE_FADE
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.
Constant Value: 2 (0x00000002)
INDICATOR_ANIMATION_MODE_LINEAR
public static final int INDICATOR_ANIMATION_MODE_LINEAR
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.
Constant Value: 0 (0x00000000)
INDICATOR_GRAVITY_BOTTOM
public static final int INDICATOR_GRAVITY_BOTTOM
Indicator gravity used to align the tab selection indicator to the bottom of the TabLayout
. 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(int)
(deprecated). Otherwise, the
indicator will not be shown. This is the default value.
Constant Value: 0 (0x00000000)
INDICATOR_GRAVITY_CENTER
public static final int INDICATOR_GRAVITY_CENTER
Indicator gravity used to align the tab selection indicator to the center of the TabLayout
. 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(int)
(deprecated). Otherwise, the
indicator will not be shown.
Constant Value: 1 (0x00000001)
INDICATOR_GRAVITY_STRETCH
public static final int INDICATOR_GRAVITY_STRETCH
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.
Constant Value: 3 (0x00000003)
INDICATOR_GRAVITY_TOP
public static final int INDICATOR_GRAVITY_TOP
Indicator gravity used to align the tab selection indicator to the top of the TabLayout
. 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(int)
(deprecated). Otherwise, the
indicator will not be shown.
Constant Value: 2 (0x00000002)
MODE_AUTO
public static final int MODE_AUTO
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.
See also:
Constant Value: 2 (0x00000002)
MODE_FIXED
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.
See also:
Constant Value: 1 (0x00000001)
MODE_SCROLLABLE
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.
See also:
Constant Value: 0 (0x00000000)
TAB_LABEL_VISIBILITY_LABELED
public static final int TAB_LABEL_VISIBILITY_LABELED
This mode is set by default. If a tab is instantiated with TabLayout.Tab.setText(CharSequence)
, a
visible label will be created.
See also:
Constant Value: 1 (0x00000001)
TAB_LABEL_VISIBILITY_UNLABELED
public static final int TAB_LABEL_VISIBILITY_UNLABELED
If a tab is instantiated with TabLayout.Tab.setText(CharSequence)
, and this mode is set, the text
will be saved and utilized for the content description, but no visible labels will be created.
See also:
Constant Value: 0 (0x00000000)
Public constructors
TabLayout
public TabLayout (Context context)
Parameters | |
---|---|
context |
Context |
TabLayout
public TabLayout (Context context, AttributeSet attrs)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
TabLayout
public TabLayout (Context context, AttributeSet attrs, int defStyleAttr)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
defStyleAttr |
int |
Public methods
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(OnTabSelectedListener)
.
Parameters | |
---|---|
listener |
TabLayout.OnTabSelectedListener : listener to add
|
addOnTabSelectedListener
public void addOnTabSelectedListener (BaseOnTabSelectedListener listener)
This method is deprecated.
use addOnTabSelectedListener(OnTabSelectedListener)
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(BaseOnTabSelectedListener)
.
Parameters | |
---|---|
listener |
BaseOnTabSelectedListener : 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 | |
---|---|
tab |
TabLayout.Tab : Tab to add
|
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 | |
---|---|
tab |
TabLayout.Tab : Tab to add |
setSelected |
boolean : True if the added tab should become the selected tab.
|
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 | |
---|---|
tab |
TabLayout.Tab : The tab to add |
position |
int : The new position of the 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 | |
---|---|
tab |
TabLayout.Tab : The tab to add |
position |
int : The new position of the tab |
setSelected |
boolean : True if the added tab should become the selected tab.
|
addView
public void addView (View child, int index)
Parameters | |
---|---|
child |
View |
index |
int |
addView
public void addView (View child, ViewGroup.LayoutParams params)
Parameters | |
---|---|
child |
View |
params |
ViewGroup.LayoutParams |
addView
public void addView (View child, int index, ViewGroup.LayoutParams params)
Parameters | |
---|---|
child |
View |
index |
int |
params |
ViewGroup.LayoutParams |
addView
public void addView (View child)
Parameters | |
---|---|
child |
View |
clearOnTabSelectedListeners
public void clearOnTabSelectedListeners ()
Remove all previously added TabLayout.OnTabSelectedListener
s.
generateLayoutParams
public FrameLayout.LayoutParams generateLayoutParams (AttributeSet attrs)
Parameters | |
---|---|
attrs |
AttributeSet |
Returns | |
---|---|
FrameLayout.LayoutParams |
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.
Parameters | |
---|---|
index |
int |
Returns | |
---|---|
TabLayout.Tab |
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.
Returns | |
---|---|
ColorStateList |
getTabIndicatorAnimationMode
public int getTabIndicatorAnimationMode ()
Get the current indicator animation mode used to animate the selection indicator between destinations.
Related XML Attributes:
Returns | |
---|---|
int |
one of INDICATOR_ANIMATION_MODE_LINEAR or INDICATOR_ANIMATION_MODE_ELASTIC |
See also:
getTabIndicatorGravity
public int getTabIndicatorGravity ()
Get the current indicator gravity used to align the tab selection indicator in the TabLayout
.
Returns | |
---|---|
int |
one of INDICATOR_GRAVITY_BOTTOM , INDICATOR_GRAVITY_CENTER , INDICATOR_GRAVITY_TOP , or INDICATOR_GRAVITY_STRETCH
|
getTabMode
public int getTabMode ()
Returns the current mode used by this TabLayout
.
Returns | |
---|---|
int |
See also:
getTabRippleColor
public ColorStateList getTabRippleColor ()
Returns the ripple color for this TabLayout.
Returns | |
---|---|
ColorStateList |
the color (or ColorStateList) used for the ripple |
See also:
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.
Returns | |
---|---|
ColorStateList |
hasUnboundedRipple
public boolean hasUnboundedRipple ()
Returns whether this TabLayout
has an unbounded ripple effect, or if ripple is bound to
the tab item size.
Related XML Attributes:
Returns | |
---|---|
boolean |
See also:
isInlineLabel
public boolean isInlineLabel ()
Returns whether tab labels will be displayed inline with tab icons, or if they will be displayed underneath tab icons.
Related XML Attributes:
Returns | |
---|---|
boolean |
See also:
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.
Related XML Attributes:
Returns | |
---|---|
boolean |
whether or not selection indicator width is fit to the full width of the tab item |
See also:
newTab
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 | |
---|---|
TabLayout.Tab |
A new Tab |
See also:
onInitializeAccessibilityNodeInfo
public void onInitializeAccessibilityNodeInfo (AccessibilityNodeInfo info)
Parameters | |
---|---|
info |
AccessibilityNodeInfo |
onInterceptTouchEvent
public boolean onInterceptTouchEvent (MotionEvent event)
Parameters | |
---|---|
event |
MotionEvent |
Returns | |
---|---|
boolean |
onTouchEvent
public boolean onTouchEvent (MotionEvent event)
Parameters | |
---|---|
event |
MotionEvent |
Returns | |
---|---|
boolean |
removeAllTabs
public void removeAllTabs ()
Remove all tabs from the tab layout and deselect the current tab.
removeOnTabSelectedListener
public void removeOnTabSelectedListener (TabLayout.OnTabSelectedListener listener)
Remove the given TabLayout.OnTabSelectedListener
that was previously added via addOnTabSelectedListener(OnTabSelectedListener)
.
Parameters | |
---|---|
listener |
TabLayout.OnTabSelectedListener : listener to remove
|
removeOnTabSelectedListener
public void removeOnTabSelectedListener (BaseOnTabSelectedListener listener)
This method is deprecated.
use removeOnTabSelectedListener(OnTabSelectedListener)
Remove the given TabLayout.BaseOnTabSelectedListener
that was previously added via
addOnTabSelectedListener(BaseOnTabSelectedListener)
.
Parameters | |
---|---|
listener |
BaseOnTabSelectedListener : 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 | |
---|---|
tab |
TabLayout.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 | |
---|---|
position |
int : Position of the tab to remove
|
selectTab
public void selectTab (TabLayout.Tab tab)
Selects the given tab.
Parameters | |
---|---|
tab |
TabLayout.Tab : The tab to select, or null to select none. |
See also:
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 | |
---|---|
tab |
TabLayout.Tab : The tab to select, or null to select none. |
updateIndicator |
boolean : Whether to update the indicator. |
See also:
setElevation
public void setElevation (float elevation)
Parameters | |
---|---|
elevation |
float |
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.
Related XML Attributes:
Parameters | |
---|---|
inline |
boolean |
See also:
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.
Related XML Attributes:
Parameters | |
---|---|
inlineResourceId |
int : Resource ID for boolean inline flag |
See also:
setOnTabSelectedListener
public void setOnTabSelectedListener (BaseOnTabSelectedListener listener)
This method is deprecated.
Use addOnTabSelectedListener(OnTabSelectedListener)
and removeOnTabSelectedListener(OnTabSelectedListener)
.
Parameters | |
---|---|
listener |
BaseOnTabSelectedListener |
setOnTabSelectedListener
public void setOnTabSelectedListener (TabLayout.OnTabSelectedListener listener)
This method is deprecated.
Use addOnTabSelectedListener(OnTabSelectedListener)
and removeOnTabSelectedListener(OnTabSelectedListener)
.
Parameters | |
---|---|
listener |
TabLayout.OnTabSelectedListener |
setScrollPosition
public void setScrollPosition (int position, float positionOffset, boolean updateSelectedTabView, boolean updateIndicatorPosition)
Set the scroll position of the TabLayout
.
Parameters | |
---|---|
position |
int : Position of the tab to scroll. |
positionOffset |
float : Value from [0, 1) indicating the offset from position . |
updateSelectedTabView |
boolean : Whether to draw the tab at the specified position + positionOffset
as selected.
Note that calling the method with |
updateIndicatorPosition |
boolean : Whether to set the indicator to the specified position and
offset.
Note that calling the method with |
See also:
setScrollPosition
public void setScrollPosition (int position, float positionOffset, boolean updateSelectedTabView)
Set the scroll position of the TabLayout
.
Parameters | |
---|---|
position |
int : Position of the tab to scroll. |
positionOffset |
float : Value from [0, 1) indicating the offset from position . |
updateSelectedTabView |
boolean : Whether to draw the tab at the specified position + positionOffset
as selected.
Note that calling the method with |
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(int)
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 | |
---|---|
tabSelectedIndicator |
Drawable : 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(int)
the selection indicator
will be tinted that color. Otherwise, it will use the colors specified in the drawable.
Parameters | |
---|---|
tabSelectedIndicatorResourceId |
int : 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 ERROR(/SlidingTabIndicator#draw(Canvas))
. 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.
Related XML Attributes:
Parameters | |
---|---|
color |
int : 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(int)
(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.
Related XML Attributes:
Parameters | |
---|---|
indicatorGravity |
int : one of INDICATOR_GRAVITY_BOTTOM , INDICATOR_GRAVITY_CENTER , INDICATOR_GRAVITY_TOP , or INDICATOR_GRAVITY_STRETCH |
setSelectedTabIndicatorHeight
public void setSelectedTabIndicatorHeight (int height)
This method is deprecated.
If possible, set the intrinsic height directly on a custom indicator drawable
passed to setSelectedTabIndicator(Drawable)
.
Sets the tab indicator's height for the currently selected tab.
Related XML Attributes:
Parameters | |
---|---|
height |
int : height to use for the indicator in pixels |
setTabGravity
public void setTabGravity (int gravity)
Set the gravity to use when laying out the tabs.
Related XML Attributes:
Parameters | |
---|---|
gravity |
int : 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.
Parameters | |
---|---|
iconTint |
ColorStateList |
See also:
setTabIconTintResource
public void setTabIconTintResource (int iconTintResourceId)
Sets the icon tint resource for the different states (normal, selected) used for the tabs.
Parameters | |
---|---|
iconTintResourceId |
int : A color resource to use as icon tint. |
See also:
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.
Related XML Attributes:
Parameters | |
---|---|
tabIndicatorAnimationMode |
int : one of INDICATOR_ANIMATION_MODE_LINEAR or INDICATOR_ANIMATION_MODE_ELASTIC |
See also:
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.
Related XML Attributes:
Parameters | |
---|---|
tabIndicatorFullWidth |
boolean : Whether or not to fit selection indicator width to full width of
the tab item |
See also:
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 aViewPager
.
Related XML Attributes:
Parameters | |
---|---|
mode |
int : 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 or below, we draw this color as a filled overlay rather than a ripple.
Related XML Attributes:
Parameters | |
---|---|
color |
ColorStateList : color (or ColorStateList) to use for the ripple |
See also:
setTabRippleColorResource
public void setTabRippleColorResource (int tabRippleColorResourceId)
Sets the ripple color resource for this TabLayout.
When running on devices with KitKat or below, we draw this color as a filled overlay rather than a ripple.
Parameters | |
---|---|
tabRippleColorResourceId |
int : A color resource to use as ripple color. |
See also:
setTabTextColors
public void setTabTextColors (int normalColor, int selectedColor)
Sets the text colors for the different states (normal, selected) used for the tabs.
Related XML Attributes:
Parameters | |
---|---|
normalColor |
int |
selectedColor |
int |
setTabTextColors
public void setTabTextColors (ColorStateList textColor)
Sets the text colors for the different states (normal, selected) used for the tabs.
Parameters | |
---|---|
textColor |
ColorStateList |
See also:
setTabsFromPagerAdapter
public void setTabsFromPagerAdapter (PagerAdapter adapter)
This method is deprecated.
Use setupWithViewPager(ViewPager)
to link a TabLayout with a ViewPager
together. When that method is used, the TabLayout will be automatically updated when the
PagerAdapter
is changed.
Parameters | |
---|---|
adapter |
PagerAdapter |
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.
Related XML Attributes:
Parameters | |
---|---|
unboundedRipple |
boolean |
See also:
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.
Related XML Attributes:
Parameters | |
---|---|
unboundedRippleResourceId |
int : Resource ID for boolean unbounded ripple value |
See also:
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(ViewPager, boolean)
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 |
autoRefresh |
boolean : whether this layout should refresh its contents if the given ViewPager's
content changes
|
shouldDelayChildPressedState
public boolean shouldDelayChildPressedState ()
Returns | |
---|---|
boolean |
Protected methods
onAttachedToWindow
protected void onAttachedToWindow ()
onDetachedFromWindow
protected void onDetachedFromWindow ()
onDraw
protected void onDraw (Canvas canvas)
Parameters | |
---|---|
canvas |
Canvas |
onMeasure
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
Parameters | |
---|---|
widthMeasureSpec |
int |
heightMeasureSpec |
int |
releaseFromTabPool
protected boolean releaseFromTabPool (TabLayout.Tab tab)
Parameters | |
---|---|
tab |
TabLayout.Tab |
Returns | |
---|---|
boolean |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2023-12-15 UTC.