ViewPager viewPager = findViewById(R.id.viewpager); viewPager.setAdapter(adapter); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // This method is called when the user begins to swipe between pages } @Override public void onPageSelected(int position) { // This method is called when a new page has been selected } @Override public void onPageScrollStateChanged(int state) { // This method is called when the scroll state changes (e.g. idle, dragging, settling) } });In this example, we have a ViewPager with a set adapter. We then add an instance of the addOnPageChangeListener to the ViewPager, which we define as an anonymous class that implements the required methods onPageScrolled, onPageSelected, and onPageScrollStateChanged. The package library for this listener is android.support.v4.view. This package contains a collection of support libraries that provide backward compatibility to newer APIs while still allowing apps to run on older devices with older APIs.