Change tab index of fragmented index tabs programmatically

I get a null pointer exception when I try to programmatically switch tabs from tablayout inside a fragment,

So, I have a main lesson that has a tab (4 tabs), each tab has a presentation pager containing a fragment, and each of these fragments has a tab layout (number of tabs) with a presentation pager containing a fragment, I can switch layout tabs main activity tab from any fragment like this

TabLayout tabLayout = MainActivity.tabLayout; TabLayout.Tab tab = tabLayout.getTabAt(2); tab.select(); 

but if I try to change the tabs of one of the fragments in the same way, I get a null pointer

 TabLayout tabLayout2 = tabFragOne.tabLayout; TabLayout.Tab tab2 = tabLayout2.getTabAt(2); tab2.select(); 

this happens only if I press the button in question when the application opens first, which indicates that the reason for this is because the fragment has not yet been attached or created,

for example, if I look at the snippets tab that I want to go to, and then return to the main activity and click on the button in question, it will work. Does anyone know a better way to fix this?

Ok ive found that half of this question is actually im using a view pager adapter, the question here sheds a lot of light on my problem

+6
source share
6 answers

The correct way to set the selected tab is to set the selected index from Pager.

 final ViewPager pager = (ViewPager) findViewById(R.id.pager); final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(pager); pager.setCurrentItem(0);//selected position 

Here is my xml layout design

 <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="#ffffff" android:background="@color/al_white" android:elevation="0dp" /> <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" app:tabTextColor="#494a43" app:tabSelectedTextColor="#494a43" app:tabIndicatorColor="#494a43" app:tabIndicatorHeight="2dp" app:tabMode="fixed" app:tabGravity="fill" /> </android.support.design.widget.AppBarLayout> 
+2
source
 TabLayout tabhost = (TabLayout) getActivity().findViewById(R.id.tabLayout); tabhost.getTabAt(2).select(); 

I use this inside my snippet creation function ... and R.id.tabLayout works R.id.tabLayout - this is the id of the tablayout in your main activity layout.

+1
source

Try the following:

  TabLayout tablayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //your activity layout tabLayout =(TabLayout)findViewById(R.id.tablayout); //your tab layout id tabLayout.getTabAt(0).select(); //your tab index; } 
0
source

you can use setCurrentItem (Postion) in viewpager Example:

 tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager)); // tabLayout.setupWithViewPager(mViewPager); mViewPager.setCurrentItem(4); 
0
source

A simple example of automatic scrolling from the last page to the first. Note. I added a dummy fragment in the first and last position.

  viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { if (position == 0) { viewPager.setCurrentItem(viewPagerAdapter.getCount() - 2); } else if (position == viewPagerAdapter.getCount() - 1) { viewPager.setCurrentItem(1); } } @Override public void onPageScrollStateChanged(int state) { } }); 
0
source

Using the presentation pager, its working simple and thin

 public class ViewPagerAdapter extends FragmentStatePagerAdapter { CharSequence Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created int NumbOfTabs; String mode; AllDeals deals; MyDeals myDeals; those are my fragments public ViewPagerAdapter(FragmentManager fm, int mNumbOfTabsumb, String moe) { super(fm); this.NumbOfTabs = mNumbOfTabsumb; mode=moe; } @Override public Fragment getItem(int position) { switch (position) { case 0: if(mode.equals("Deals")){ deals=new AllDeals(); return deals; } break; case 1: if(mode.equals("Deals")){ myDeals=new MyDeals(); return myDeals; } break; default: break; } return null; } @Override public int getCount() { return NumbOfTabs; } } 

add these lines to ur activity

  containerViewone.setVisibility(View.VISIBLE); tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.title_all_deals))); tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.title_my_deals))); tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); tabLayout.setTabTextColors(getResources().getColor(R.color.tabunselect), getResources().getColor(R.color.black)); viewpagerad = new ViewPagerAdapter(getSupportFragmentManager(), Numboftabs, "Deals"); pager.setAdapter(viewpagerad); pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); 
0
source

All Articles