I am looking to add a horizontal scroll view for an Android TabLayout.
The TabLayout has multiple tabs in it and is scrollable. Due to multiple tabs on it, some of them are not visible at the first glance. The users have to scroll to get to the tabs on the far right(which are usually hidden) and so those tabs are not getting user's attention.
The thought is to have a horizontal scroll indicator or an arrow indicating that there are more tabs to the right so the users can scroll to find/use them.
The design idea is to have a scrollIndicator and not have a tabIndicator. I found the following image from Google which is closer to the idea.
Thanks in advance,
Horizontal scrolling can be achieved by clicking and dragging a horizontal scroll bar, swiping sideways on a desktop trackpad or trackpad mouse, pressing left and right arrow keys, or swiping sideways with one's finger on a touchscreen.
For horizontal scrollable bar use the x and y-axis. Set the overflow-y: hidden; and overflow-x: auto; that will automatically hide the vertical scroll bar and present only the horizontal scrollbar. The white-space: nowrap; property is used to wrap text in a single line.
HorizontalScrollView is used to scroll the child elements or views in a horizontal direction. HorizontalScrollView only supports horizontal scrolling. For vertical scroll, android uses ScrollView.
To create horizontal scrollable table layout on your screen, you have put your table layout in HorizontalScrollView. In this layout, we have created a horizontal line at the bottom of table row and to to customize scrollbar, we have created a custom drawable for it named - scroll_indicator and set it as scrollbarThumbIndicator.
This method is deprecated. Use addOnTabSelectedListener (OnTabSelectedListener) and removeOnTabSelectedListener (OnTabSelectedListener) . 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 .
Tabs are created using newTab () method of TabLayout class. The title and icon of Tabs are set through setText (int) and setIcon (int) methods of TabListener interface respectively. Tabs of layout are attached over TabLayout using the method addTab (Tab) method. We can also add tab item to TabLayout using TabItem of android design widget.
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. Set the gravity to use when laying out the tabs. int: one of GRAVITY_CENTER or GRAVITY_FILL.
You can achieve what you want by encapsulating the tab layout inside a horizontal scroll view like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:scrollbars="horizontal">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
app:tabGravity="center"
app:tabIndicator="@color/white"
app:tabMode="fixed">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 1"/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 2"/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 3"/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 4"/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 5"/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 6"/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 7"/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 8"/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 9"/>
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab 10"/>
</com.google.android.material.tabs.TabLayout>
</HorizontalScrollView>
</LinearLayout>
It will look like this ......
Hi you can add app:tabMode="scroll"
in an example use it as
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scroll"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With