Roman Roman - 1 year ago 110
Android Question

How to change ViewPager's page?

I'm using ViewPager in my app and define it in the main Activity. Inside

method I load some number of pages from SharedPreferences and then pass it to PagerAdapter:

public int getCount() {
return numberOfPages;

The problem is that if I would change this number in Preferences (or another Activity) to some other less then page index I viewed before, my app crashes because this index is out of bounds when I return to the activity with this ViewPager. It can be fixed simply by changing active ViewPager's page. Is there any way to do it?

Answer Source

I'm not sure that I fully understand the question, but from the title of your question, I'm guessing that what you're looking for is pager.setCurrentItem( num ). That allows you to programatically switch to another page within the ViewPager.

I'd need to see a stack trace from logcat to be more specific if this is not the problem.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download