Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between PagerTabStrip and TabLayout

Tags:

I am working on an app where I want to add tabs so that can be added using PagerTabStrip and TabLayout with ViewPager.

It looks same to me with no difference but I guess there is some difference between them so they are two classes for this. So what is the main difference between them?

like image 347
N Sharma Avatar asked Feb 27 '16 10:02

N Sharma


People also ask

What is TabLayout?

com.google.android.material.tabs.TabLayout. TabLayout provides a horizontal layout to display tabs.

Can we use TabLayout without ViewPager in Android?

These methods have a features: always attaching the "tab widget" with a ViewPager , and in order to make this requirement, we must disable swipe feature of the ViewPager . Now, with Material design, we now use TabLayout widget, which can "stand alone" to build a tab bar and do not need a ViewPager anymore.

Can we use TabLayout in fragment in Android?

You will have to deal with nested fragments inside pager. All page content must be there and TabLayout must be in the same layout where you ViewPager is.


2 Answers

Just comparing the visuals...

TabLayout is a material concept that replaced the deprecated ActionBar tabs in Android 5.0.
It extends HorizontalScrollView, so you can keep adding tabs horizontally which can include text, icons, or custom views and scroll through them linearly without paging.

TabLayout provides the setupWithViewPager(ViewPager viewPager) method to attach to a ViewPager instead of being part of the ViewPager like the PagerTabStrip.

TabLayout

A PagerTabStrip is more of an indictor for the current page of a ViewPager, and "it is intended to be used as a child view of a ViewPager widget". The scrolling for it does not act like the TabLayout since each tab is part of the page instead of individually horizontally scrollable.

PagerTabStrip

In summary, the differences are (apart from visuals).

  1. A TabLayout is part of the Activity/Fragment, outside of the ViewPager, and you instead attach it to one. It scrolls separately from the pages.
  2. A PagerTabStrip is a child element of the ViewPager. The tabs scroll with the page.
like image 64
OneCricketeer Avatar answered Oct 16 '22 17:10

OneCricketeer


From the Developer.android I can say that the TabLayout is the material design concept and as all knows it's new concept than the PagerTabStrip.

You can check the base hierarchy for both

1) TabLayout Hirarchy

java.lang.Object
   ↳    android.view.View
       ↳    android.view.ViewGroup
           ↳    android.widget.FrameLayout
               ↳    android.widget.HorizontalScrollView
                   ↳    android.support.design.widget.TabLayout

2) PagerTabStrip Hirarchy

java.lang.Object
   ↳    android.view.View
       ↳    android.view.ViewGroup
           ↳    android.support.v4.view.PagerTitleStrip
               ↳    android.support.v4.view.PagerTabStrip

For more detail check TabLayout and PagerTabStrip

like image 29
Ajay Pandya Avatar answered Oct 16 '22 15:10

Ajay Pandya