I have added TabLayout
(from support library v22.2.1) to my Fragment as:
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
style="@style/MyColorAccentTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"/>
The issue is that when the Fragment's orientation is landscape (before or after the initial creation of the fragment), the TabLayout
doesn't match the width of the Fragment
(yes the parent has its width set to match_parent
as well).
When screen width is small (i.e not all tabs can be shown at same time):
When screen width is big enough to show all tabs (see the blank space at the right):
If I change tabMode
to fixed, width is filled but tabs are too small. Is there any proper solution out there?
I was struggling as well and most options above did not work for me, so I created a version on the answer of DTX12.
I have a boolean to check for tablet or not and make a check on landscape and portrait. Then set the layout based on amount of tabs.
You might need to make the method public and call it on rotation / configurationchange.
In the CustomTabLayout Code i replaced the mintabswidth method
Instead of creating custom TabLayout and hacking around or creating more layouts which acts as wrapper around TabLayout only for background. Try this,
This will set background to behind tabLayout instead of setting background behind every tab.
I guess this is the simpliest way to achieve what you want.
You may used app:tabMaxWidth="0dp" in tablayout
I didn't care about the tabs filling the width, but I cared that the background color wasn't expanding to the full width. So I thought of this solution, where I put a FrameLayout behind it with the same background color as the tabs.
try with this changes