In this blog on Android ViewPager Fragment Example, you’ll see Android ViewPager Fragment Example in which we will create fragments on activity.

Need some career advice or prepping for an Android developer interview? Hit me up on Topmate.io, and let's chat!

android ViewPager Fragment Example
Android ViewPager Fragment

In an Android app, anyone can create fragments. These are just views on an activity with different behavior which can be used to build a multi-pane User interface.

However, the best part about fragments are that they can be reused in multiple activities.

Android ViewPager Fragment Example

Here is a step by step tutorial on Android ViewPager Fragment Example:

What we will create

android ViewPager Fragment Example
Android ViewPager Fragment Example

Getting Started

  • Open Android Studio and create a new Project.
  • Create an Empty Blank Activity in the project.
  • Wait for Gradle to build and then go to Project in the Left Corner.

Creating the required file

  • Have Android Selected in the Project Drop down menu and go to your package name in the Java folder (app > java > ‘com.cpmpanyname.appname’)
  • Right click on the folder and then go to New > Java Class Name this class as ViewPagerAdapter as keep visibility as public with no modifiers.
  • Once the class is created open it and extend the class to FragmentPagerAdapter.
  • Now click inside curly braces and press Ctrl + I and then implement the respective methods: getItem(position) and getCount();
Android ViewPager Fragment Example
Methods to implement

Let’s Code

  • Again go and click on one of the curly braces of class and now press Alt + Insert and select ‘Constructor’, create an empty constructor.
  • Now create two Array list, one ArrayList<String> for Tab titles and another ArrayList<Fragment> for fragments.
  • Now create addFragments method for the Array Lists.
  • In the getItem() method return fragments.get(position) and in getCount() method return fragments.size().
  • Now in the class ViewPagerAdapter, override a method by clicking Alt + Insert and click override method and search for public CharSequence getPageTitle(int position). In this method return tabTitles.get(position);
  • TaDa! Now the ViewPagerAdapter class is ready and now add some fragments by going to app in project on the right and right click on ‘app’ and then go to New > Fragment > Fragment (Blank).
  • Now go to activity_main.xml or activity where you want to inflate the fragments.
  • Create AppBarLayout for TabLayout where tabs will be created and viewPager after AppBarLayout where Fragments will be inflated by using the code shown below:
<android.support.design.widget.AppBarLayout    
   android:id="@+id/toolbar"    
   android:layout_width="match_parent"    
   android:layout_height="wrap_content"    
   android:fitsSystemWindows="true"    
   android:minHeight="?attr/actionBarSize"    
   android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.TabLayout        
       android:id="@+id/tablayout"        
       android:layout_width="match_parent"        
       android:layout_height="wrap_content"        
       app:tabBackground="?attr/selectableItemBackground"        
       app:tabGravity="fill"        
       app:tabIndicatorColor="@color/colorPrimaryDark"        
       app:tabIndicatorHeight="5dp"        
       app:tabMode="scrollable">
    
    </android.support.design.widget.TabLayout>

</android.support.design.widget.AppBarLayout>

  <android.support.v4.view.ViewPager    
       android:id="@+id/viewPager"    
       android:layout_width="match_parent"    
       android:layout_height="match_parent">

  </android.support.v4.view.ViewPager>
  • Now go to activity where you want to inflate these fragments and create three objects there.
TabLayout tablayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;

// Inside onCreate() method
tablayout = findViewById(R.id.tablayout);
viewPager = findViewById(R.id.viewPager);


// viewPagerAdapter.addFragments(new fragment_name(), "Fragment Name to Appear in Tab");
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(new Last7Days(), "Last 7 Days");
viewPagerAdapter.addFragments(new last30days(), "Last 30 Days");
viewPagerAdapter.addFragments(new last90days(), "Last 90 Days");
viewPagerAdapter.addFragments(new last365days(), "Last 365 Days");
viewPager.setAdapter(viewPagerAdapter);tablayout.setupWithViewPager(viewPager);

Now you’ve successfully created and inflated fragments. 

Learn more about Android ImageView | Basic Understanding and 6 Great things To Do

Here we will wrap up this tutorial on Android ViewPager Fragment Example. In case you face any issues please comment below we will help resolve your issues.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

You May Also Like