Getting the current position of a ViewPager

My solution would only work if you have a TabLayout linked to your ViewPager.

This is how they're linked:

tabLayout.setupWithViewPager(viewPager);

And then to get current position, you can make use of:

tabLayout.getSelectedTabPosition()

You can use:

mViewPager.getCurrentItem()

Update 2019

Now you can set addOnPageChangeListener on View Pager to Observe change in Page position.

Since you wanted to setup a listener and retrieve the position when the page is switched

mViewPager.addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {}
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}

            override fun onPageSelected(position: Int) {
                pagePosition.setText("" + position + "/" + galleryAdapter!!.count)
            }
        })

Create a listener and set it on your viewpager:

/**
 * Get the current view position from the ViewPager by
 * extending SimpleOnPageChangeListener class and adding your method
 */
public class DetailOnPageChangeListener extends ViewPager.SimpleOnPageChangeListener {

    private int currentPage;

    @Override
    public void onPageSelected(int position) {
        currentPage = position;
    }

    public final int getCurrentPage() {
        return currentPage;
    }
}