Tablayout Tabs Text Not Displaying
Solution 1:
The problem is you're calling setupWithViewPager()
after setting up your tabs with the addTab()
calls, effectively overwriting them.
From the documentation of TabLayout
regarding setupWithViewPager()
:
The tabs displayed in this layout will be populated from the
ViewPager
adapter's page titles.
If you would like to use your TabLayout
with a ViewPager
, you should override getPageTitle()
in your PagerAdapter
(and remove the addTab()
calls, they are redundant).
For example:
publicclassExamplePagerAdapterextendsFragmentStatePagerAdapter {
// tab titlesprivate String[] tabTitles = newString[]{"Tab1", "Tab2", "Tab3"};
publicExamplePagerAdapter(FragmentManager fm) {
super(fm);
}
// overriding getPageTitle()@Overridepublic CharSequence getPageTitle(int position) {
return tabTitles[position];
}
@Overridepublic Fragment getItem(int position) {
switch (position) {
case0:
returnnewTab1Fragment();
case1:
returnnewTab2Fragment();
case2:
returnnewTab3Fragment();
default:
thrownewRuntimeException("Invalid tab position");
}
}
@OverridepublicintgetCount() {
return tabTitles.length;
}
// ...
}
Solution 2:
i added tab text and icon for each tab after calling tabs.setupWithViewPager(viewPager)
viewPager.setAdapter(new MyViewAdapter(getSupportFragmentManager()));
TabLayout tabs=(TabLayout) findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
tabs.getTabAt(0).setIcon(R.drawable.icon1);
tabs.getTabAt(1).setIcon(R.drawable.icon2);
tabs.getTabAt(0).setText(getResources().getText(R.string.tab1));
tabs.getTabAt(1).setText(getResources().getText(R.string.tab2));
Solution 3:
I'm using Kotlin and have spent an hour to find the bugs with this code.
var viewPager: ViewPager = view.findViewById(R.id.viewPager)
...
adapter = TabAdapter(fragmentManager!!)
adapter.addFragment(Fragment1(), "Fragment 1")
adapter.addFragment(Fragment2(), "Fragment 2")
viewPager.adapter = adapter
tabLayout.setupWithViewPager(viewpager)
I can compile this with no error, but the tab title is not showing until I realize the viewpager I assigned to the tabLayout is not viewPager (notice the capitalize 'P'). It's not producing error because kotlin will find any matching layout id, in this case "viewpager" which is a ViewPager but on another view.
Solution 4:
I wanted to define TabItem in XML instead of doing it programmatically. As you mentioned, this setupWithViewPager() call resets the text and icon of the tab items to null( It uses the same tab object, just resets its content). I used your approach but made it more reusable. Here is my code:
publicclassTabLayoutUtil {
publicstaticvoidsetupTabLayoutWithViewPager(TabLayout tabLayout, ViewPager viewPager) {
ArrayList<Pair<CharSequence, Drawable>> tabsContentCopy= new ArrayList<>();
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
tabsContentCopy.add(new Pair<>(tab != null ? tab.getText() : null, tab != null ? tab.getIcon() : null));
}
tabLayout.setupWithViewPager(viewPager);
for (int i = 0; i < tabLayout.getTabCount(); i++) {
if (i<tabsContentCopy.size()) {
tabLayout.getTabAt(i).setText(tabsContentCopy.get(i).first);
tabLayout.getTabAt(i).setIcon(tabsContentCopy.get(i).second);
}
}
}
}
Post a Comment for "Tablayout Tabs Text Not Displaying"