Tab Layout
(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 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.