Bashalex
Bashalex

Reputation: 325

Android support library 23.2. TabLayout with icons

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?

Upvotes: 2

Views: 386

Answers (1)

marmor
marmor

Reputation: 28219

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);
...

Upvotes: 1

Related Questions