How do you get the current page number of a ViewPager for Android?

AndroidAndroid Viewpager

Android Problem Overview


I want to know the current page position of the ViewPager. I create an adapter and set this adapter to the ViewPager. Now I want to know the current page number or position of the current view, because I want to start some extra events on that particular page.

I used viewPager.setCurrentItem(1); for setting the first item.

Is there a similar method for getting the current page?

Android Solutions


Solution 1 - Android

in the latest packages you can also use

vp.getCurrentItem()

or

vp is the viewPager ,

pageListener = new PageListener();
vp.setOnPageChangeListener(pageListener);

you have to put a page change listener for your viewPager. There is no method on viewPager to get the current page.

private int currentPage;

    private static class PageListener extends SimpleOnPageChangeListener{
    		public void onPageSelected(int position) {
    			Log.i(TAG, "page selected " + position);
                   currentPage = position;
    	}
    }

Solution 2 - Android

There is a method object_of_ViewPager.getCurrentItem() which returns the position of currently Viewed page of view pager

Solution 3 - Android

If you only want the position, vp.getCurrentItem() will give it to you, no need to apply the onPageChangeListener() for that purpose alone.

Solution 4 - Android

You will figure out that setOnPageChangeListener is deprecated, use addOnPageChangeListener, as below:

ViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
       if(position == 1){  // if you want the second page, for example
           //Your code here
       }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
});

Solution 5 - Android

For this problem Onpagechange listener is the best one But it will also have one small mistake that is it will not detect the starting time time of 0th position Once you will change the page it will starts to detect the Page selected position...For this problem I fount the easiest solution

1.You have to maintain the selected position value then use it....
2. Case 1: At the starting of the position is always Zero....
Case 2: Suppose if you set the current item means you will set that value into maintain position
3.Then do your action with the use of that maintain in your activity...

Public int maintain=0;
myViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i2) {
        	 //Toast.makeText(MyActivity.this, i+"  Is Selected  "+data.size(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onPageSelected( int i) {
          // here you will get the position of selected page
        	maintain = i;
        	 
        	
        }

        @Override
        public void onPageScrollStateChanged(int i) {
        	
        }
    });


updateButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
           
            Toast.makeText(MyActivity.this, i+"  Is Selected  "+data.size(), Toast.LENGTH_SHORT).show();
            data.set(maintain, "Replaced "+maintain);         
            myViewPager.getAdapter().notifyDataSetChanged();
        }
    });

Solution 6 - Android

getCurrentItem(), doesn't actually give the right position for the first and the last page I fixed it adding this code:

public void CalcPostion() {    
    current = viewPager.getCurrentItem();

    if ((last == current) && (current != 1) && (current != 0)) {
        current = current + 1;
        viewPager.setCurrentItem(current);
    }
    if ((last == 1) && (current == 1)) {
        last = 0;
        current = 0;
    }
    display();
    last = current;
}

Solution 7 - Android

> The setOnPageChangeListener() method is deprecated. Use > addOnPageChangeListener(OnPageChangeListener) instead.

You can use OnPageChangeListener and getting the position inside onPageSelected() method, this is an example:

   viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            Log.d(TAG, "my position is : " + position); 
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

Or just use getCurrentItem() to get the real position:

viewPager.getCurrentItem();

Solution 8 - Android

There is no any method getCurrentItem() in viewpager.i already checked the API

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsam_kView Question on Stackoverflow
Solution 1 - AndroidYashwanth KumarView Answer on Stackoverflow
Solution 2 - AndroidKrishna ShresthaView Answer on Stackoverflow
Solution 3 - Androiddevanshu_kaushikView Answer on Stackoverflow
Solution 4 - AndroidGeraldo NetoView Answer on Stackoverflow
Solution 5 - AndroidNaveen Kumar KuppanView Answer on Stackoverflow
Solution 6 - AndroidDa-monkView Answer on Stackoverflow
Solution 7 - AndroidJorgesysView Answer on Stackoverflow
Solution 8 - AndroidramanView Answer on Stackoverflow