I am using the TabLayout from Android Design support library. How can I use MODE_SCROLLABLE along with GRAVITY_FILL so that it looks the same across all devices and orientations?
Currently if I use MODE_SCROLLABLE, it looks good on 7" tablet, but on 10" tablet, the tabs are aligned to the left. I want to be able to use GRAVITY_FILL for all sizes.
This is my tablayout:
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="72dp"
/>
and I am setting the mode in code
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
Click the Default box for the Tab Scrolling setting. Now select one of the tabs shrink to options: pinned, medium, or large width setting. Alternatively, you can select a tabs don't shrink or Disabled option on that drop-down menu. Press Relaunch after selecting a tab width setting.
A TabLayout provides a way to display tabs horizontally.
Per the TabLayout.GRAVITY_FILL documentation:
Gravity used to fill the
TabLayout
as much as possible. This option only takes effect when used with MODE_FIXED.
Therefore by setting your mode to MODE_SCROLLABLE, tabs are always aligned from the starting edge - the only difference between tablets and phones for MODE_SCROLLABLE is the minimum width of tabs, as set by the tabs material design spec.
Note that you can specify both gravity and tab mode via XML if you want to set the view to always be scrollable:
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="72dp"
app:tabMode="scrollable"
/>
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