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.



            app:layout_behavior="@string/appbar_scrolling_view_behavior"  />


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!


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 ~10 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