2

There are no icons in tabs if app built with Support Library 23.2.0

I have an application with tabLayout with icons.

@Override
public CharSequence getPageTitle(int position) {
    Drawable image = ContextCompat.getDrawable(context, imageResId[position]);
    image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
    SpannableString sb = new SpannableString(" ");
    ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
    sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return sb;
}

I use this code in my FragmentPagerAdapter and it worked nice before Android Support Library 23.2.0. I mean this code works correctly while I use version 23.1.1 for example, but there are clean tabs without icons if I build app with support library version 23.2.0. Is it a temporary bug that will be fixed in the next revisions or should I implement tabs with icons in some another way?

Bashalex
  • 325
  • 2
  • 11
  • Hi, have you solved your problem? I've got exactly same issue... I've refactored me code with @marmor solution.. but I still wonder what's wrong with above code... – Rafal Malek Mar 04 '16 at 12:37
  • Hi, unfortunately no. I downgraded the version back to 23.1.1. Hopefully it's a bug in that version of support library and it will be fixed in the next one. – Bashalex Mar 04 '16 at 14:37

1 Answers1

1

There's an API for setting icons to TabLayout tabs:

...
tabLayout.setupWithViewPager(pager);
tabLayout.getTabAt(0).setIcon(R.drawable.icon1);
tabLayout.getTabAt(1).setIcon(R.drawable.icon2);
...
marmor
  • 27,641
  • 11
  • 107
  • 150
  • Yes, I've tried this method as well. But there is another problem with it. It works correctly until the first call of notifyDataSetChanged() inside the FragmentPagerAdapter (I use it when change fragments in one of the page). After this call icons disappear. By the way, this bug arise only with Support library 23.2.0 as well. – Bashalex Mar 03 '16 at 20:35