ViewPager is a layout manager that enables users to navigate through pages of data by swiping left and right. It is commonly seen in apps like YouTube and Snapchat where users can switch screens by swiping. Fragments are utilized instead of activities, and ViewPager is also employed to onboard users when they first launch the app.
Guidelines for implementing ViewPager:
Guidelines for implementing ViewPager:An adapter fills the pages within the ViewPager. PagerAdapter serves as the base class extended by FragmentPagerAdapter and FragmentStatePagerAdapter. Let's explore the key distinctions between these two classes.
Distinguishing features of FragmentPagerAdapter and FragmentStatePagerAdapter:
Distinguishing features of FragmentPagerAdapter and FragmentStatePagerAdapter:| import androidx.annotation.NonNull; |
| import androidx.annotation.Nullable; |
| import androidx.fragment.app.Fragment; |
| import androidx.fragment.app.FragmentManager; |
| import androidx.fragment.app.FragmentPagerAdapter; |
| import java.util.ArrayList; |
| import java.util.List; |
ViewPagerAdapter class is used to manage the fragments displayed within a ViewPager. It extends FragmentPagerAdapter, a class provided by the Android Support Library for managing fragments within a ViewPager.
Within the ViewPagerAdapter class, fragments and their titles are stored in separate lists. The add() method is used to add fragments along with their titles to the adapter.
The getItem() method is overridden to return the fragment at a specific position within the adapter. The getCount() method returns the total number of fragments in the adapter.
The getPageTitle() method is overridden to return the title of the fragment at a specified position, which is used as the tab title when the ViewPager is displayed with tabs.
By using ViewPagerAdapter, developers can efficiently manage and display multiple fragments within a ViewPager, providing a seamless user experience when navigating between different screens or sections of an application.
A sample GIF is given below to provide an overview of the project that will be implemented using Java:

To initiate a new project in Android Studio, follow the steps outlined in How to Create/Start a New Project in Android Studio. Ensure that Java is selected as the programming language.

Begin by working with the activity_main.xml file. The file includes the following components:
Refer to the image below for important parameters to set:

In the TabLayout, ensure to add the parameter tabmode="fixed" in the activity_main.xml file for a fixed number of tabs in the application.

Below is the XML code snippet to be added to the activity_main.xml file:
| <?xml version="1.0" encoding="utf-8"?> |
| <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> |
| <com.google.android.material.appbar.AppBarLayout android:id="@id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content"> |
| <com.google.android.material.tabs.TabLayout android:id="@id/tab_layout" android:layout_width="match_parent" android:layout_height="match_parent" app:tabGravity="fill" app:tabMode="fixed" /> |
| <androidx.viewpager.widget.ViewPager android:id="@id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> |
| </androidx.coordinatorlayout.widget.CoordinatorLayout> |
Lastly, proceed with creating fragments to further develop the application.
Below are the code snippets for Page1.java, Page2.java, and Page3.java files respectively:
import android.os.Bundle; | import android.view.LayoutInflater; | import android.view.View; |
import android.view.ViewGroup; | Page1 extends Fragment { | public Page1() { |
// Required empty public constructor. | onCreate(Bundle savedInstanceState) { | super.onCreate(savedInstanceState); |
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | return inflater.inflate(R.layout.fragment_page1, container, false); |
import android.os.Bundle; | import android.view.LayoutInflater; | import android.view.View; |
import android.view.ViewGroup; | Page2 extends Fragment { | public Page2() { |
// Required empty public constructor. | onCreate(Bundle savedInstanceState) { | super.onCreate(savedInstanceState); |
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | return inflater.inflate(R.layout.fragment_page2, container, false); |
The Page3 class in Android is a fragment that plays a crucial role in the Android app development lifecycle. Fragments are essential components for building flexible and modular user interfaces.
When designing the XML files for pages in Android, consistency is key. All fragment XML layouts typically share identical designs. A central TextView often displays the page's name. The root container is usually a FrameLayout, with a background color commonly set to #0F9D58.
import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; public class Page3 extends Fragment { public Page3() { // required empty public constructor. } @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_page3, container, false); } } |

Code:
"Page 3" 
In the ViewPagerAdapter.java file, we define a class named ViewPagerAdapter, which extends FragmentPagerAdapter. This class is responsible for managing the fragments displayed in a ViewPager.
In the MainActivity.java file, we typically handle the setup and behavior of the main activity of an Android application. This includes activities like initializing components, handling user interactions, and coordinating with other parts of the app.
Syntax: TabLayout.setupWithViewPager(ViewPager pager).
Description: This method links the TabLayout with the ViewPager. Each pager's title appears on the TabLayout, allowing users to navigate through fragments by clicking on the tabs.
Parameter:
import android.os.Bundle; | import androidx.appcompat.app.AppCompatActivity; |
import androidx.viewpager.widget.ViewPager; | import com.google.android.material.tabs.TabLayout; |
Below is a simplified explanation of the code:
AppCompatActivity class.ViewPagerAdapter, ViewPager, and TabLayout.onCreate method, set the content view, find the ViewPager by ID, and set up the adapter.setupWithViewPager.By following these steps, the MainActivity is properly set up to display fragments with titles on the TabLayout, allowing seamless navigation for the user.
Find the code in the following GitHub repo: GitHub Repository
Ready to start an exciting journey into Android Development with Kotlin? Begin a fantastic learning experience with our Mastering Android Development with Kotlin From Beginner to Pro - Self Paced!