I am trying to use the new Design TabLayout in my project. I want the layout to adapt to every screen size and orientation, but it can be seen correctly in one orientation.
I am dealing with Gravity and Mode setting my tabLayout as:
tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
So I expect that if there is no room, the tabLayout is scrollable, but if there is room, it is centered.
From the guides:
public static final int GRAVITY_CENTER Gravity used to lay out the tabs in the center of the TabLayout.
public static final int GRAVITY_FILL Gravity used to fill the TabLayout as much as possible. This option only takes effect when used with MODE_FIXED.
public static final int MODE_FIXED Fixed tabs display all tabs concurrently and are best used with content that benefits from quick pivots between tabs. The maximum number of tabs is limited by the view’s width. Fixed tabs have equal width, based on the widest tab label.
public static final int MODE_SCROLLABLE Scrollable tabs display a subset of tabs at any given moment, and can contain longer tab labels and a larger number of tabs. They are best used for browsing contexts in touch interfaces when users don’t need to directly compare the tab labels.
So GRAVITY_FILL is compatible only with MODE_FIXED but, at is doesn't specify anything for GRAVITY_CENTER, I expect it to be compatible with MODE_SCROLLABLE, but this is what I get using GRAVITY_CENTER and MODE_SCROLLABLE
So it is using SCROLLABLE in both orientations, but it is not using GRAVITY_CENTER.
This is what I would expect for landscape; but to have this, I need to set MODE_FIXED, so what I get in portrait is:
Why is GRAVITY_CENTER not working for SCROLLABLE if the tabLayout fits the screen? Is there any way to set gravity and mode dynamically (and to see what I am expecting)?
Thank you very much!
EDITED: This is the Layout of my TabLayout:
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:background="@color/orange_pager"
android:layout_height="wrap_content" />
This is the only code that worked for me:
The DisplayMetrics can be retrieved using this:
And your TabLayout XML should look like this (don't forget to set tabMaxWidth to 0):
I solved this using following
I created an AdaptiveTabLayout class to achieve this. This was the only way I found to actually solve the problem, answer the question and avoid/workaround problems that other answers here don't.
Notes:
MODE_SCROLLABLE
but not enough room forMODE_FIXED
. If you don't handle this case it's gonna happen on some devices you'll see different text sizes or oven two lines of text in some tabs, which looks bad.wrap_content
on the xml. Don't set any mode or gravity on the xml.AdaptiveTabLayout.java
And this is the DeviceUtils class:
Use example:
Gist
Problems/Ask for help:
Logcat:
I'm not sure how to solve it. Any suggestions?
this is how i did it
TabLayout.xml
Oncreate
I made small changes of @Mario Velasco's solution on the runnable part
Tab gravity only effects
MODE_FIXED
.One possible solution is to set your
layout_width
towrap_content
andlayout_gravity
tocenter_horizontal
:If the tabs are smaller than the screen width, the
TabLayout
itself will also be smaller and it will be centered because of the gravity. If the tabs are bigger than the screen width, theTabLayout
will match the screen width and scrolling will activate.This is the solution I used to automatically change between
SCROLLABLE
andFIXED
+FILL
. It is the complete code for the @Fighter42 solution:(The code below shows where to put the modification if you've used Google's tabbed activity template)
Layout:
If you don't need to fill width, better to use @karaokyo solution.