1

I have a TabLayout that I'm using along with AppBarLayout, ToolBar, and ViewPager in order to have scrollable tabs. At the moment, I have a single tab, but rather than being left aligned the tab is centered.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

        <android.support.design.widget.TabLayout
            android:id="@+id/tablayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>
Jean Vitor
  • 893
  • 1
  • 18
  • 24
Jill
  • 533
  • 7
  • 22

3 Answers3

5

Set your TabLayout attributes to: app:tabGravity="center" app:tabMode="scrollable"

hope this help.

Jermaine
  • 770
  • 7
  • 9
0

Try using the XML attribute set

android:layout_gravity="left"

This is all that I could find on this topic it is documented

here

that set layout_gravity is a value for a tabbed layout.

More documentation here. And here

  • That didn't work. Also, my TabLayout is under AppBarLayout not ViewPager. I believe the align left is for the entire TabLayout and not for the individual tabs also. – Jill Aug 04 '16 at 23:37
  • Okay I see. Have you tried aligning it with a parent? layout_alignParentLeft? – Fidel_Willis Aug 04 '16 at 23:38
  • Just tried it - no luck. The problem is any layout parameters I changed I think effect the entire TabLayout not the tabs. There are parameters like `app:tabContentStart` but those don't to be doing anything either. – Jill Aug 04 '16 at 23:42
  • You said you only have one tab? Why would you want it to be left aligned? That seems like it wouldnt look that good? – Fidel_Willis Aug 04 '16 at 23:43
  • Haha let's not get into a design debate. It looks kind of weird when it's centered. I'd prefer left aligned – Jill Aug 04 '16 at 23:49
  • Okay so I know that this way might be a little more than you would have liked. But I have been trying to solve this problem and the only way that seems to actually move the tab to me is through utilizing the margin attribute. And since all screen sizes vary. I would suggest that you implement a method to get the screen dimensions. A post of this can be found [here](http://stackoverflow.com/questions/6589101/how-to-get-screen-size-of-device). And then once you have the width of the screen you could divide that by three and then set the margin right or left (Which ever you prefer) to that. – Fidel_Willis Aug 05 '16 at 00:15
  • Thanks for the suggestions. There's no margin attribute for the tabs apparently. There is however padding. But when you add padding, then the line that is below the tab (the one which indicates that it is selected) becomes too long. – Jill Aug 05 '16 at 01:27
0

Just change TabLayout widhth to wrap_content... that will make that single tab to be left aligned

Adam reuben
  • 45
  • 1
  • 4