In this article, we will learn about how to add TabLayout with ViewPager in an app. TabLayout provides a horizontal layout to display tabs. If TabLayout is used then along with it, Fragment is also used, because fragments are light weight and the app can have more functionality on a single screen if more fragments are added. Whenever the user clicks on the tab it will lead to the transaction of one Fragment to another. ViewPager is used to swipe between the tabs. WhatsApp, Facebook, etc are a very good example of TabLayout with ViewPager. This is how a TabLayout looks like.
Approach:
- Create a AlgorithmFragment by right click on java package,
 select new -> fragment -> select Fragment(Blank).
- Follow above step for CourseFragment and LoginFragment.
- Now add the following code in AlgorithmFragment.xml file. Here a TextView is added in the layout.
- Now add the following code in CourseFragment.xml file. Here a textView is added in the layout.
- Now add the following code in LoginFragment.xml file. Here a textView is added in the layout.
- Now Create a Adapter for our ViewPager by extending FragmentPagerAdapter class. In this class we will override three methods getItem(), getCount() and getPageTitle(). getItem method gives the fragment with respect to the position, getCount method gives total number of fragments present and getPageTitle method gives the title of the fragment.
- Add the following code in MainActivity.java file. In this file we setup our adapter and attach our TabLayout with ViewPager.
AlgorithmFragment.xml<?xmlversion="1.0"encoding="utf-8"?><FrameLayout   Âandroid:layout_width="match_parent"   Âandroid:layout_height="match_parent"   Âtools:context=".AlgorithmFragment">   Â<TextView       Âandroid:textColor="@color/colorPrimary"       Âandroid:textSize="30sp"       Âandroid:gravity="center"       Âandroid:layout_width="match_parent"       Âandroid:layout_height="match_parent"       Âandroid:text="Learn Algorithm\nVisit GFG"/>ÂÂ</FrameLayout>CourseFragment.xml<?xmlversion="1.0"encoding="utf-8"?><FrameLayout   Âandroid:layout_width="match_parent"   Âandroid:layout_height="match_parent"   Âtools:context=".CourseFragment">   Â<TextView       Âandroid:textColor="@color/colorPrimary"       Âandroid:textSize="30sp"       Âandroid:gravity="center"       Âandroid:layout_width="match_parent"       Âandroid:layout_height="match_parent"       Âandroid:text="Select Course by GFG"/>ÂÂ</FrameLayout>LoginFragment.xml<?xmlversion="1.0"encoding="utf-8"?><FrameLayout   Âandroid:layout_width="match_parent"   Âandroid:layout_height="match_parent"   Âtools:context=".LoginFragment">   Â<TextView       Âandroid:textColor="@color/colorPrimary"       Âandroid:textSize="30sp"       Âandroid:gravity="center"       Âandroid:layout_width="match_parent"       Âandroid:layout_height="match_parent"       Âandroid:text="GFG Login Portal"/>ÂÂ</FrameLayout>ViewPagerAdapter.javapackageorg.neveropen.gfgtablayout;ÂÂimportandroidx.annotation.NonNull;importandroidx.fragment.app.Fragment;importandroidx.fragment.app.FragmentManager;importandroidx.fragment.app.FragmentPagerAdapter;ÂÂpublicclassViewPagerAdapterÂextendsFragmentPagerAdapter {   ÂpublicViewPagerAdapter(@NonNullFragmentManager fm)   Â{       Âsuper(fm);   Â}   Â@NonNull   Â@Override   ÂpublicFragment getItem(intposition)   Â{       ÂFragment fragment =null;       Âif(position ==0)           Âfragment =newAlgorithmFragment();       Âelseif(position ==1)           Âfragment =newCourseFragment();       Âelseif(position ==2)           Âfragment =newLoginFragment();       Âreturnfragment;   Â}   Â@Override   ÂpublicintgetCount()   Â{       Âreturn3;   Â}   Â@Override   ÂpublicCharSequence getPageTitle(intposition)   Â{       ÂString title =null;       Âif(position ==0)           Âtitle ="Algorithm";       Âelseif(position ==1)           Âtitle ="Courses";       Âelseif(position ==2)           Âtitle ="Login";       Âreturntitle;   Â}}MainActivity.javapackageorg.neveropen.gfgtablayout;ÂÂimportandroidx.appcompat.app.AppCompatActivity;importandroidx.viewpager.widget.ViewPager;importandroid.os.Bundle;importandroid.widget.Toolbar;ÂÂimportcom.google.android.material.tabs.TabLayout;ÂÂpublicclassMainActivityÂextendsAppCompatActivity {   ÂTabLayout tabLayout;   ÂViewPager viewPager;   ÂViewPagerAdapter viewPagerAdapter;   Â@Override   ÂprotectedvoidonCreate(Bundle savedInstanceState)   Â{       Âsuper.onCreate(savedInstanceState);       ÂsetContentView(R.layout.activity_main);       ÂviewPager = findViewById(R.id.view_pager);       ÂtabLayout = findViewById(R.id.tabs);       ÂviewPagerAdapter =newViewPagerAdapter(           ÂgetSupportFragmentManager());       ÂviewPager.setAdapter(viewPagerAdapter);       Â// It is used to join TabLayout with ViewPager.       ÂtabLayout.setupWithViewPager(viewPager);   Â}}
Output:

 
                                    









[…] will start by adding the TabLayout and ViewPager in the activity_main.xml. After doing this, we will add three blank fragments to the […]