Background
I've used the PagerSlidingTabStrip library for a long time, to show tabs above a ViewPager.
Recently, I was tasked to set icons (using selectors, with selected-vs-unselected states) instead of texts for the tabs, and so I did. However, it seems that the library couldn't handle it well, showing empty tabs sometimes, so I've moved to TabLayout, which is a part of the design library by Google
The problem
I've noticed a few solutions of how to add icons to the TabLayout, but each of them has one or more of those issues :
- no icons are shown
- icons are shown, but can blink from time to time, especially when using a selector for them with "exitFadeDuration" being set, or when swiping fast
- clicking on tabs does not change the current page of the viewPager.
The code
The code I've used is from cheesesquare sample, in the MainActivity.java file. It's quite basic:
final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
...
tabLayout.setupWithViewPager(viewPager);
The solutions I've tried are:
setting an icon for each tab (and remove "getPageTitle" code of the adapter) :
for (int i = 0; i < tabLayout.getTabCount(); i++) tabLayout.getTabAt(i).setIcon(...);
Also tried adding setOnTabSelectedListener for when I didn't use a selector.
This solution results in blinking effect (issue #2).
Extending TabLayout to support icons or implementing TabViewProvider, as shown here.
Extending TabLayout doesn't show icons at all (issue #1), and implementing TabViewProvider has the issue of blinking icons (quite rare though).
For getPageTitle, return a SpannableString that has the icon in it, as shown here . This didn't show the icons at all for me.
I remember I tried other solutions too, but they also had issues as I've mentioned.
The question
What is the correct way to set icons for the tabs?
Is there an official way to achieve this? I'd like to at least have a selected&unselected images for each tab. The transition of when selecting is a good bonus, which I've hoped to achieve as it looks nicer this way.
Why do the icons blink anyway? I've noticed that it occurs even for texts...
Is there maybe a workaround?