1

I am going to implement scrolling feature to my TabLayout. It is scrolling successfully but the tab gravity is not working at all. My tabs are aligned to right side of the view. I want to show those tabes in the central position or at least fill the layout_width.

I added my codes below. Please suggest the possible way to make things working.

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_scrollFlags="scroll|enterAlways|snap"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        app:tabMode="scrollable"
        app:tabGravity="fill"/>

</android.support.design.widget.AppBarLayout>

and here is the screen shot of the tablayout.

enter image description here

anuradha
  • 692
  • 1
  • 9
  • 22
  • check this link:http://stackoverflow.com/questions/30616474/android-support-design-tablayout-gravity-center-and-mode-scrollable – prakash Jul 22 '16 at 13:42
  • @prakash neither android:layout_gravity="center_horizontal" nor app:tabGravity="fill" worked for me. I saw the link you provided. – anuradha Jul 22 '16 at 13:52

1 Answers1

0

The magic resides on the gravity mode, in your case you want the GRAVITY_CENTER. Change your code to this:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_scrollFlags="scroll|enterAlways|snap"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        app:tabMode="scrollable"
        app:tabGravity="center"/>

</android.support.design.widget.AppBarLayout>
Joaquim Ley
  • 4,038
  • 2
  • 24
  • 42