Skip to main content

Android ViewPager Fragment Example | Implement Unlimited Fragments

In Android anyone can create fragments that are views on an activity with different behavior which can be used to build a multi-pane User interface. In this blog you'll see Android ViewPager Fragment Example in which we will create fragments on activity.

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:

Android ViewPager Fragment Example
Android ViewPager Fragment Example

  • 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.
  • 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
Android ViewPager Fragment Example

  • 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 code shown below:

<    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: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:id="@+id/viewPager"    android:layout_width="match_parent"    android:layout_height="match_parent">

  • Now go to activity where you want to inflate these fragments and create two variables there. 

TabLayout tablayout;
ViewPager viewPager;ViewPagerAdapter viewPagerAdapter;

// Inside onCreate() method
tablayout = findViewById(;viewPager = findViewById(;

  • Now write following lines of code:

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);

In the code above fragments can be added by using following line of code:

viewPagerAdapter.addFragments(new fragment_name(), "Fragment Name to Appear in Tab");

Now you've successfully created and inflated fragments. 

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


Popular posts from this blog

How To Open PDF File in Android Application Example Included

Want to know How To Open PDF File in Android Application Example. To open a PDF File in Android Application, your app can take help from Free Android library available on Github.

Here are few apps made from PDF Library you can look at, click here to know more.

By using Android Library you are not only leveraging the hard work from another developer but also making it easier and quicker to develop an android app.
How To Open PDF File in Android Application Example  In this tutorial series, we will teach you how to open a pdf file in Android Application Example included with the help of Android PDFViewer library developed by Barteksc.

Get inspirtion from here: Top 7 PDF Download for Mobile Android Apps

Before I proceed further, here is the screenshot of the kind of PDF Viewer that’ll be created using Android Studio.

 How To Open PDF File in Android Application Example

The app will open a specified pdf file which will be copied in the assets folder of your Android studio project. I'v…

Open Weather Map API Example | Create Android Weather App

In this tutorial blog, using open weather map API example, we will create an Android weather app. The Android App will able to access Open weather map website server and access weather details of particular location.

The App will look similar to the image shown below. It will fetch real time weather of the location provided in MainActivity.Java file.

Software / Files used:Android Studio (Version 3.x) Files Edited / Created:activity_main.xmlMainActivity.JavaWeather.JavaAndroidManifest.xml Steps Involved in Open Weather Map API Example: Here are the Steps you need to follow on ‘Open Weather map API Example’ to create Android Weather App:

1. Open the Open Weather map API link provided here. Sign up for Current Weather Data API key here.

2. Once the key is received, save it to notepad or on stick notes.

3. Open Android Studio and create a new Project with name, ‘FIMC Weather App’, we will select an empty activity in the wizard and wait for Gradle build.

4. Open AndroidManifest.xml…

Android Live Wallpaper Tutorial (using Android Studio 3.x) | FaultInMyCode

The Android Live Wallpapers are the way to redesign your mobile background into something amazing animated GIF using different effects and motions. Many novice developers are making good money by Henceforth here is Android Live Wallpaper tutorial for you to build your own Live wallpaper and start earning today as a developer!

This tutorial is a part of our Blog series on ‘Roadway to Android Development Course’, its suitable for Beginners to programming as well as the android enthusiast to learn about Android development and build their own apps.

In this tutorial for beginners, you’ll learn how by using Android Studio 3.0 and by having basic knowledge of Java, we will teach you to build your own Live wallpaper for Android which you may later publish in the Google Play Store. We assume you have some experience with Android studio, if not please check how to build your own calculator app and how to create simple login app in Android Studio 3.x tutorial.
How to build Android …