I am using android support design tablayout. Here's my code:
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content""
app:tabGravity="center"
app:tabMode="scrollable"
/>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
My issue is the tabs always align left. However, I would like to center the selected tab (even at the beginning, the first(selected) tab should be centered). Is there a way to do this? Thanks.
Perhaps you are looking for an xml attribute called
app:tabContentStart
. This attribute allows you push the active tab to the left according to the value specified, which is set in "dp" (kind of like a margin-left thing). For example, try putting your code like this:It doesn't exactly center the tab, but it gives you a similar effect as the Google Play Store tabs.
I took a look into TabLayout and tabContentStart only sets padding for its first child -> SlidingTabStrip, so I set it manually on both sides:
TabLayout's first 0 index child is the SlidingTabStrip.
If your target API is 23 or above, let me share one more option for consideration along with the configuration of
app:tabContentStart
suggested by SynerFlow, which could let the end Tab be centered instead of swiped to the left-most edge during scrolling. However, I am still figuring out the approach for API before 23.