Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 289
Chris Byatt Avatar asked Aug 20 '14 15:08

Chris Byatt


1 Answers

Got it. Needed to use my ViewPager instead of TabHost

ViewPager viewPager = (ViewPager) getActivity().findViewById(R.id.pager);
viewPager.setCurrentItem(2);
like image 132
Chris Byatt Avatar answered Oct 02 '22 10:10

Chris Byatt