TabLayout

(SESL Variant) 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.

See also

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

Constructors

Link copied to clipboard
constructor(@NonNull context: Context)
constructor(@NonNull context: Context, @Nullable attrs: AttributeSet)
constructor(@NonNull context: Context, @Nullable attrs: AttributeSet, defStyleAttr: Int)

Types

Link copied to clipboard
Callback interface invoked when a tab's selection state changes.
Link copied to clipboard
annotation class LabelVisibility
Link copied to clipboard
annotation class Mode
Link copied to clipboard
Callback interface invoked when a tab's selection state changes.
Link copied to clipboard
open class Tab
A tab in this layout.
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
A ViewPager.OnPageChangeListener class which contains the necessary calls back to the provided TabLayout so that the tab position is kept in sync.
Link copied to clipboard
inner class TabView : LinearLayout
A LinearLayout containing Tab instances for use with TabLayout.
Link copied to clipboard
A TabLayout.OnTabSelectedListener class which contains the necessary calls back to the provided ViewPager so that the tab position is kept in sync.

Properties

Link copied to clipboard
Gravity used to lay out the tabs in the center of the TabLayout.
Link copied to clipboard
val GRAVITY_FILL: Int = 0
Gravity used to fill the TabLayout as much as possible.
Link copied to clipboard
Gravity used to lay out the tabs aligned to the start of the TabLayout.
Link copied to clipboard
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.
Link copied to clipboard
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.
Link copied to clipboard
Indicator animation mode used to translate the selected tab indicator between two tabs using a linear motion.
Link copied to clipboard
Indicator gravity used to align the tab selection indicator to the bottom of the .
Link copied to clipboard
Indicator gravity used to align the tab selection indicator to the center of the .
Link copied to clipboard
Indicator gravity used to stretch the tab selection indicator across the entire height of the TabLayout.
Link copied to clipboard
Indicator gravity used to align the tab selection indicator to the top of the .
Link copied to clipboard
val MODE_AUTO: Int = 2
Auto-sizing tabs behave like MODE_FIXED with GRAVITY_CENTER while the tabs fit within the TabLayout's content width.
Link copied to clipboard
val MODE_FIXED: Int = 1
Fixed tabs display all tabs concurrently and are best used with content that benefits from quick pivots between tabs.
Link copied to clipboard
Scrollable tabs display a subset of tabs at any given moment, and can contain longer tab labels and a larger number of tabs.
Link copied to clipboard
Auto-sizing tabs behave like MODE_FIXED with GRAVITY_FILL while the tabs fit within the TabLayout's content width.
Link copied to clipboard
Auto-sizing tabs behave like MODE_FIXED with GRAVITY_FILL while the tabs fit within the TabLayout's content width.
Link copied to clipboard
This mode is set by default.
Link copied to clipboard
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.
Link copied to clipboard
open var tabGravity: Int
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
Add a TabLayout.BaseOnTabSelectedListener that will be invoked when tab selection changes.
Add a TabLayout.OnTabSelectedListener that will be invoked when tab selection changes.
Link copied to clipboard
open fun addTab(@NonNull tab: TabLayout.Tab)
open fun addTab(@NonNull tab: TabLayout.Tab, setSelected: Boolean)
open fun addTab(@NonNull tab: TabLayout.Tab, position: Int)
open fun addTab(@NonNull tab: TabLayout.Tab, position: Int, setSelected: Boolean)
Add a tab to this layout.
Link copied to clipboard
open fun addView(child: View)
open fun addView(child: View, params: ViewGroup.LayoutParams)
open fun addView(child: View, index: Int)
open fun addView(child: View, index: Int, params: ViewGroup.LayoutParams)
Link copied to clipboard
Remove all previously added TabLayout.OnTabSelectedListeners.
Link copied to clipboard
Returns the position of the current selected tab.
Link copied to clipboard
Returns the tab at the specified index.
Link copied to clipboard
open fun getTabCount(): Int
Returns the number of tabs currently registered with the tab layout.
Link copied to clipboard
open fun getTabMode(): Int
Returns the current mode used by this TabLayout.
Link copied to clipboard
Returns the ripple color for this TabLayout.
Link copied to clipboard
Returns whether this TabLayout has an unbounded ripple effect, or if ripple is bound to the tab item size.
Link copied to clipboard
Returns whether tab labels will be displayed inline with tab icons, or if they will be displayed underneath tab icons.
Link copied to clipboard
Get whether or not selection indicator width is fit to full width of the tab item, or fit to the tab item's content.
Link copied to clipboard
Create and return a new Tab.
Link copied to clipboard
Link copied to clipboard
open fun onTouchEvent(event: MotionEvent): Boolean
Link copied to clipboard
open fun removeAllTabs()
Remove all tabs from the tab layout and deselect the current tab.
Link copied to clipboard
Remove a tab from the layout.
Link copied to clipboard
open fun removeTabAt(position: Int)
Remove a tab from the layout.
Link copied to clipboard
open fun selectTab(@Nullable tab: TabLayout.Tab, updateIndicator: Boolean)
Selects the given tab.
Link copied to clipboard
Link copied to clipboard
open fun seslSetBadgeColor(color: Int)
Link copied to clipboard
open fun seslSetBadgeTextColor(color: Int)
Link copied to clipboard
open fun seslSetIconTextGap(gap: Int)
Link copied to clipboard
open fun seslSetSubTabIndicatorHeight(heightPixel: Int)
Link copied to clipboard
Link copied to clipboard
open fun seslSetTabSubTextColors(defaultColor: Int, selectedColor: Int)
Link copied to clipboard
open fun seslSetTabTextColor(textColor: ColorStateList, updateTabView: Boolean)
Link copied to clipboard
open fun seslSetTabWidth(width: Int)
Link copied to clipboard
open fun seslShowBadge(index: Int, show: Boolean, content: String)
open fun seslShowBadge(index: Int, show: Boolean, content: String, contentDescription: String)
Link copied to clipboard
open fun seslShowDotBadge(index: Int, show: Boolean)
open fun seslShowDotBadge(index: Int, show: Boolean, badgeDescription: String)
Link copied to clipboard
Link copied to clipboard
open fun setInlineLabel(inline: Boolean)
Set whether tab labels will be displayed inline with tab icons, or if they will be displayed underneath tab icons.
Link copied to clipboard
open fun setInlineLabelResource(@BoolRes inlineResourceId: Int)
Set whether tab labels will be displayed inline with tab icons, or if they will be displayed underneath tab icons.
Link copied to clipboard
open fun setScrollPosition(position: Int, positionOffset: Float, updateSelectedTabView: Boolean)
open fun setScrollPosition(position: Int, positionOffset: Float, updateSelectedTabView: Boolean, updateIndicatorPosition: Boolean)
Set the scroll position of the TabLayout.
Link copied to clipboard
open fun setSelectedTabIndicator(@Nullable tabSelectedIndicator: Drawable)
Sets the selection indicator for this TabLayout.
open fun setSelectedTabIndicator(@DrawableRes tabSelectedIndicatorResourceId: Int)
Sets the drawable resource to use as the selection indicator for this TabLayout.
Link copied to clipboard
Sets the tab indicator's color for the currently selected tab.
Link copied to clipboard
open fun setSelectedTabIndicatorGravity(indicatorGravity: Int)
Set the indicator gravity used to align the tab selection indicator in the TabLayout.
Link copied to clipboard
Sets the tab indicator's height for the currently selected tab.
Link copied to clipboard
open fun setTabIconTintResource(@ColorRes iconTintResourceId: Int)
Sets the icon tint resource for the different states (normal, selected) used for the tabs.
Link copied to clipboard
open fun setTabIndicatorFullWidth(tabIndicatorFullWidth: Boolean)
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.
Link copied to clipboard
open fun setTabMode(mode: Int)
Set the behavior mode for the Tabs in this layout.
Link copied to clipboard
Sets the ripple color for this TabLayout.
Link copied to clipboard
open fun setTabRippleColorResource(@ColorRes tabRippleColorResourceId: Int)
Sets the ripple color resource for this TabLayout.
Link copied to clipboard
Link copied to clipboard
open fun setTabTextColors(normalColor: Int, selectedColor: Int)
Sets the text colors for the different states (normal, selected) used for the tabs.
Link copied to clipboard
open fun setUnboundedRipple(unboundedRipple: Boolean)
Set whether this TabLayout will have an unbounded ripple effect or if ripple will be bound to the tab item size.
Link copied to clipboard
open fun setUnboundedRippleResource(@BoolRes unboundedRippleResourceId: Int)
Set whether this TabLayout will have an unbounded ripple effect or if ripple will be bound to the tab item size.
Link copied to clipboard
open fun setupWithViewPager(@Nullable viewPager: ViewPager)
open fun setupWithViewPager(@Nullable viewPager: ViewPager, autoRefresh: Boolean)
The one-stop shop for setting up this TabLayout with a ViewPager.