Search code examples
androidandroid-viewpagerandroid-tablayout

TabLayout click tab manually


I have a ViewPagerAdapter like below.

    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

    adapter.addFragment(new Fragment1(), "ONE");
    adapter.addFragment(new Fragment2(), "TWO");
    adapter.addFragment(new Fragment3(), "THREE");
    adapter.addFragment(new Fragment4(), "FOUR");

I am attaching my viewpagesAdapter to a TabLayout

 tabLayout = (TabLayout) findViewById(R.id.tabs);
 tabLayout.setupWithViewPager(viewPager);

And then, I'm hiding the tab-4

 ((ViewGroup) tabLayout.getChildAt(0)).getChildAt(3).setVisibility(View.GONE);

My question is how I can trigger the click of tab-4 in the new button's click event. Is there a way to do this?


Solution

  • To click a tab programatically use:

    viewPager.setCurrentItem(index);
    

    In your case index = 3;