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 |
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.
You can also add items to TabLayout in your layout through the use of TabItem
.
An example usage is like so:
<android.support.design.widget.TabLayout android:layout_height="wrap_content" android:layout_width="match_parent"> <android.support.design.widget.TabItem android:text="@string/tab_text"/> <android.support.design.widget.TabItem android:icon="@drawable/ic_android"/> </android.support.design.widget.TabLayout>
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:
<android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.TabLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" /> </android.support.v4.view.ViewPager>
See also:
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 |
|
class |
TabLayout.ViewPagerOnTabSelectedListener
A |
XML attributes | ||
---|---|---|
android.support.design:tabBackground |
Reference to a background to be applied to tabs. | |
android.support.design:tabContentStart |
Position in the Y axis from the starting edge that tabs should be positioned from. | |
android.support.design:tabGravity |
Gravity constant for tabs. | |
android.support.design:tabIndicatorColor |
Color of the indicator used to show the currently selected tab. | |
android.support.design:tabIndicatorHeight |
Height of the indicator used to show the currently selected tab. | |
android.support.design:tabMaxWidth |
The maximum width for tabs. | |
android.support.design:tabMinWidth |
The minimum width for tabs. | |
android.support.design:tabMode |
The behavior mode for the Tabs in this layout
Must be one of the following constant values. |
|
android.support.design:tabPadding |
The preferred padding along all edges of tabs. | |
android.support.design:tabPaddingBottom |
The preferred padding along the bottom edge of tabs. | |
android.support.design:tabPaddingEnd |
The preferred padding along the end edge of tabs. | |
android.support.design:tabPaddingStart |
The preferred padding along the start edge of tabs. | |
android.support.design:tabPaddingTop |
The preferred padding along the top edge of tabs. | |
android.support.design:tabSelectedTextColor |
The text color to be applied to the currently selected tab. | |
android.support.design:tabTextAppearance |
A reference to a TextAppearance style to be applied to tabs. | |
android.support.design:tabTextColor |
The default text color to be applied to tabs. |
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 |
int |
GRAVITY_FILL
Gravity used to fill the |
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. |
Inherited constants |
---|
From
class
android.view.ViewGroup
|
From
class
android.view.View
|
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
|
addOnTabSelectedListener(TabLayout.OnTabSelectedListener listener)
Add a |
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)
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)
Adds a child view. |
void
|
addView(View child)
Adds a child view. |
void
|
addView(View child, ViewGroup.LayoutParams params)
Adds a child view with the specified layout parameters. |
void
|
addView(View child, int index, ViewGroup.LayoutParams params)
Adds a child view with the specified layout parameters. |
FrameLayout.LayoutParams
|
generateLayoutParams(AttributeSet attrs)
Returns a new set of layout parameters based on the supplied attributes set. |
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 |
ColorStateList
|
getTabTextColors()
Gets the text colors for the different states (normal, selected) used for the tabs. |
TabLayout.Tab
|
newTab()
Create and return a new |
void
|
removeAllTabs()
Remove all tabs from the action bar and deselect the current tab. |
void
|
removeOnTabSelectedListener(TabLayout.OnTabSelectedListener listener)
Remove the given |
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 listener)
This method is deprecated.
Use |
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(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
|
setupWithViewPager(ViewPager viewPager, boolean autoRefresh)
The one-stop shop for setting up this |
void
|
setupWithViewPager(ViewPager viewPager)
The one-stop shop for setting up this |
boolean
|
shouldDelayChildPressedState()
Return true if the pressed state should be delayed for children or descendants of this ViewGroup. |
Protected methods | |
---|---|
void
|
onAttachedToWindow()
This is called when the view is attached to a window. |
void
|
onDetachedFromWindow()
This is called when the view is detached from a window. |
void
|
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
Measure the view and its content to determine the measured width and the measured height. |
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
|
Reference to a background to be applied to tabs.
May be a reference to another resource, in the form
"@[+][package:]type/name
" or a theme
attribute in the form
"?[package:]type/name
".
Position in the Y axis from the starting edge that tabs should be positioned from.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp
".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
Gravity constant for tabs.
Must be one of the following constant values.
Constant | Value | Description |
---|---|---|
center | 1 | |
fill | 0 |
Related methods:
Color of the indicator used to show the currently selected tab.
May be a color value, in the form of "#rgb
",
"#argb
", "#rrggbb
#aarrggbb".
Related methods:
Height of the indicator used to show the currently selected tab.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp
".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
Related methods:
The maximum width for tabs.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp
".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
The minimum width for tabs.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp
".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
The behavior mode for the Tabs in this layout
Must be one of the following constant values.
Constant | Value | Description |
---|---|---|
fixed | 1 | |
scrollable | 0 |
Related methods:
The preferred padding along all edges of tabs.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp
".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
The preferred padding along the bottom edge of tabs.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp
".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
The preferred padding along the end edge of tabs.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp
".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
The preferred padding along the start edge of tabs.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp
".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
The preferred padding along the top edge of tabs.
May be a dimension value, which is a floating point number appended with a
unit such as "14.5sp
".
Available units are: px (pixels), dp (density-independent pixels),
sp (scaled pixels based on preferred font size), in (inches), and
mm (millimeters).
The text color to be applied to the currently selected tab.
May be a color value, in the form of "#rgb
",
"#argb
", "#rrggbb
#aarrggbb".
Related methods:
A reference to a TextAppearance style to be applied to tabs.
May be a reference to another resource, in the form
"@[+][package:]type/name
" or a theme
attribute in the form
"?[package:]type/name
".
The default text color to be applied to tabs.
May be a color value, in the form of "#rgb
",
"#argb
", "#rrggbb
#aarrggbb".
Related methods:
int GRAVITY_CENTER
Gravity used to lay out the tabs in the center of the TabLayout
.
See also:
Constant Value: 1 (0x00000001)
int GRAVITY_FILL
Gravity used to fill the TabLayout
as much as possible. This option only takes effect
when used with MODE_FIXED
.
See also:
Constant Value: 0 (0x00000000)
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)
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)
TabLayout (Context context, AttributeSet attrs)
Parameters | |
---|---|
context |
Context
|
attrs |
AttributeSet
|
TabLayout (Context context, AttributeSet attrs, int defStyleAttr)
Parameters | |
---|---|
context |
Context
|
attrs |
AttributeSet
|
defStyleAttr |
int
|
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
|
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.
|
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
|
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
|
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.
|
void addView (View child, int index)
Adds a child view. If no layout parameters are already set on the child, the default parameters for this ViewGroup are set on the child.
Note: do not invoke this method from
draw(android.graphics.Canvas)
, onDraw(android.graphics.Canvas)
,
dispatchDraw(android.graphics.Canvas)
or any related method.
Parameters | |
---|---|
child |
View :
the child view to add |
index |
int :
the position at which to add the child |
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
draw(android.graphics.Canvas)
, onDraw(android.graphics.Canvas)
,
dispatchDraw(android.graphics.Canvas)
or any related method.
Parameters | |
---|---|
child |
View :
the child view to add |
void addView (View child, ViewGroup.LayoutParams params)
Adds a child view with the specified layout parameters.
Note: do not invoke this method from
draw(android.graphics.Canvas)
, onDraw(android.graphics.Canvas)
,
dispatchDraw(android.graphics.Canvas)
or any related method.
Parameters | |
---|---|
child |
View :
the child view to add |
params |
ViewGroup.LayoutParams :
the layout parameters to set on the child
|
void addView (View child, int index, ViewGroup.LayoutParams params)
Adds a child view with the specified layout parameters.
Note: do not invoke this method from
draw(android.graphics.Canvas)
, onDraw(android.graphics.Canvas)
,
dispatchDraw(android.graphics.Canvas)
or any related method.
Parameters | |
---|---|
child |
View :
the child view to add |
index |
int :
the position at which to add the child or -1 to add last |
params |
ViewGroup.LayoutParams :
the layout parameters to set on the child
|
FrameLayout.LayoutParams generateLayoutParams (AttributeSet attrs)
Returns a new set of layout parameters based on the supplied attributes set.
Parameters | |
---|---|
attrs |
AttributeSet :
the attributes to build the layout parameters from |
Returns | |
---|---|
FrameLayout.LayoutParams |
an instance of ViewGroup.LayoutParams or one
of its descendants
|
int getSelectedTabPosition ()
Returns the position of the current selected tab.
Returns | |
---|---|
int |
selected tab position, or -1 if there isn't a selected tab.
|
TabLayout.Tab getTabAt (int index)
Returns the tab at the specified index.
Parameters | |
---|---|
index |
int
|
Returns | |
---|---|
TabLayout.Tab |
int getTabCount ()
Returns the number of tabs currently registered with the action bar.
Returns | |
---|---|
int |
Tab count |
int getTabGravity ()
The current gravity used for laying out tabs.
Returns | |
---|---|
int |
one of GRAVITY_CENTER or GRAVITY_FILL .
|
ColorStateList getTabTextColors ()
Gets the text colors for the different states (normal, selected) used for the tabs.
Returns | |
---|---|
ColorStateList |
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:
void removeAllTabs ()
Remove all tabs from the action bar and deselect the current tab.
void removeOnTabSelectedListener (TabLayout.OnTabSelectedListener listener)
Remove the given TabLayout.OnTabSelectedListener
that was previously added via
addOnTabSelectedListener(OnTabSelectedListener)
.
Parameters | |
---|---|
listener |
TabLayout.OnTabSelectedListener :
listener to remove
|
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
|
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
|
void setOnTabSelectedListener (TabLayout.OnTabSelectedListener listener)
This method is deprecated.
Use addOnTabSelectedListener(OnTabSelectedListener)
and
removeOnTabSelectedListener(OnTabSelectedListener)
.
Parameters | |
---|---|
listener |
TabLayout.OnTabSelectedListener
|
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 |
int :
current scroll position |
positionOffset |
float :
Value from [0, 1) indicating the offset from position . |
updateSelectedText |
boolean :
Whether to update the text's selected state.
|
void setSelectedTabIndicatorColor (int color)
Sets the tab indicator's color for the currently selected tab.
Related XML Attributes:
Parameters | |
---|---|
color |
int :
color to use for the indicator |
void setSelectedTabIndicatorHeight (int height)
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 |
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 . |
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
.Related XML Attributes:
Parameters | |
---|---|
mode |
int :
one of MODE_FIXED or MODE_SCROLLABLE . |
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
|
void setTabTextColors (ColorStateList textColor)
Sets the text colors for the different states (normal, selected) used for the tabs.
Parameters | |
---|---|
textColor |
ColorStateList
|
See also:
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
|
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
|
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
|
boolean shouldDelayChildPressedState ()
Return true if the pressed state should be delayed for children or descendants of this ViewGroup. Generally, this should be done for containers that can scroll, such as a List. This prevents the pressed state from appearing when the user is actually trying to scroll the content. The default implementation returns true for compatibility reasons. Subclasses that do not scroll should generally override this method and return false.
Returns | |
---|---|
boolean |
void onAttachedToWindow ()
This is called when the view is attached to a window. At this point it
has a Surface and will start drawing. Note that this function is
guaranteed to be called before onDraw(android.graphics.Canvas)
,
however it may be called any time before the first onDraw -- including
before or after onMeasure(int, int)
.
void onDetachedFromWindow ()
This is called when the view is detached from a window. At this point it no longer has a surface for drawing.
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 |
int :
horizontal space requirements as imposed by the parent.
The requirements are encoded with
View.MeasureSpec . |
heightMeasureSpec |
int :
vertical space requirements as imposed by the parent.
The requirements are encoded with
View.MeasureSpec . |