Adding Padding in Tabs in Android

In the app, which I am making at my job, I had a situation where I needed a huge number of tabs with the Fragment View Pager. I used a typical TabLayout and ViewPager with a custom FragmentPagerAdapter class as the adapter for ViewPager.



This is TabLayout and ViewPager in XML layout.

<android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.AppBarLayout>
    
        <android.support.v7.widget.Toolbar/>
        
        <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                app:tabMode="scrollable"/>
    
    </<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>

But, as I run it, this is how it looked like:

Tabs without Padding Tabs without Padding

But, in Android Support library, there are two attributes for TabLayout which can fix this issue very easily. Just add these two lines in TabLayout and you’re done!

app:tabPaddingStart="10dp"
app:tabPaddingEnd="10dp"

Now, when you run, app will show like this:

Tabs with Padding Tabs with Padding


If you liked this article, you can read my new articles below:


profile card
Wajahat Karim
🌍 Making the world a better place, one app at a time.
🔥 Google Developer Expert (GDE) in Android . 📱 Professional Android Developer with ~7 years experience. 💻 Creator of various Open Source libraries on Android . 📝 Author of two technical books and 100+ articles on Android. 🎤 A passionate Public Speaker giving talks all over the world.
Author's picture

Wajahat Karim

🔥 Google Dev Expert (GDE) in Android .
📱 Android Dev. 💻 Open Source Contributor . 📝 Technical Writer . 🎤 Public Speaker

Senior Android Developer

Karachi, Pakistan