Search code examples
androidandroid-viewpagerandroid-tablayout

Difference between Tab Layout and ViewPager?


I'm learning about tab layout because I need to use tab layout.

When i search for tab layout on Google, the one that comes up together is the view pager.

By the way, I'm not sure about the difference between tablayout and viewpager.

The tab layout has a menu at the top and screen switching is possible.

Although the view pager does not have tabs, it is possible to slide the screen just like the tab layout.

But why use the two together?

What's different?

Isn't it possible to slide in tab layout as well?


Solution

  • Yes, you can use TabLayout without viewPager. But without viewpager you won't be able to slide your page. You have to handle tabselect listener manually using this method then you have to click on the tab to navigate.

    addOnTabSelectedListener(OnTabSelectedListener)
    
    

    Likewise, you can use only Viewpager then you will be able to slide or swipe your page without any tabs.

    From official doc

    TabLayout

    TabLayout provides a horizontal layout to display tabs.

    Viewpager

    Layout manager that allows the user to flip left and right through pages of data.

    So Tablayout and viewpager are two different things but you can have the privilege of both tabs and slide only by combining them.

    You can check here for tablaout without viewPager

    TabLayout without using ViewPager