Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a horizontal scroll indicator for Android TabLayout

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. enter image description here

Thanks in advance,

like image 393
user3543477 Avatar asked Nov 20 '20 19:11

user3543477


People also ask

How do you indicate horizontal scrolling?

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.

How do I create a horizontal scrolling container?

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.

What is horizontal scroll view in Android?

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.

How to create horizontal scrollable table layout on your screen?

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.

How to set the scroll position of the tabs?

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 .

How to add tabs to tablayout in Android Design widget?

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.

How do I set the height of a selected tab indicator?

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.


2 Answers

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 ......

enter image description here

like image 52
AgentP Avatar answered Sep 28 '22 01:09

AgentP


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>
like image 41
Kuldeep Avatar answered Sep 28 '22 01:09

Kuldeep