14

I tried this tutorial and it works great, but now I would like to know if it is possible, when opening the application, to go directly to a specific tab and not the first one (at left) by default ?

Ex : I would like the application to open directly on "events" tab :

enter image description here

I searched into the code, but didn't find anything relevant.

Thanks folks

EDIT : Here is the way : Just add pager.setCurrentItem(1); in your MainActivity (the tabs container).

Edric
  • 24,639
  • 13
  • 81
  • 91
Rob
  • 748
  • 2
  • 7
  • 16
  • 6
    mViewPager.setCurrentItem(i); You should write in onCreate() method your activity class. mViewPager.setCurrentItem(1); – msevgi Feb 04 '15 at 13:54

4 Answers4

25

ViewPager is most often used in conjunction with Fragment, which is a convenient way to supply and manage the lifecycle of each page.

You can use setCurrentItem // Set the currently selected page.

Set the currently selected page. If the ViewPager has already been through its first layout with its current adapter there will be a smooth animated transition between the current item and the specified item.

Finally,

Your_View_Pager_Obj.setCurrentItem(1); // call into onCreate(Bundle savedInstanceState) 
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

Use the following code along with the viewPager and tablayout

pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
silverFoxA
  • 4,549
  • 7
  • 33
  • 73
0

you can use setCurrentTab(index) in onResume() of MainActivity where index is the index of tab you want to go to.

Abdul Mateen
  • 1,418
  • 1
  • 14
  • 32
0

Add to the MainActivity.java

private static final int EVENTS = 1;

private ViewPager mViewPager;
mViewPager = (ViewPager) findViewById(R.id.pager);

@Override
    public void onStart() {
        super.onStart();
        mViewPager.setCurrentItem(EVENTS);

    }
Dharman
  • 30,962
  • 25
  • 85
  • 135
V. Hruaia
  • 29
  • 2