javaandroidandroid-fragmentsandroid-tabhostfragmentpageradapter

Change Tab Programmatically from Fragment with FragmentPagerAdapter


I'm trying to get my app to change from tab 1 to tab 3. The tabs are in a custom TabsPagerAdapter which extends FragmentPagerAdapter.

I've tried to change the tab likes this but it causes NullPointerException. Is the mechanism different with a FragmentPagerAdapter?

TabHost host = (TabHost) getActivity().findViewById(android.R.id.tabhost);
host.setCurrentTab(2);

Solution

  • Got it. Needed to use my ViewPager instead of TabHost

    ViewPager viewPager = (ViewPager) getActivity().findViewById(R.id.pager);
    viewPager.setCurrentItem(2);