Search code examples
androidandroid-tabs

How to add Icons to SlidingTabLayout?


I made sliding tabs using this lesson: http://www.exoguru.com/android/material-design/navigation/android-sliding-tabs-with-material-design.html How to use icons instead of text in Sliding tabs?


Solution

  • I change this function in ViewPagerAdapter.java and add my icon:

    @Override
     public CharSequence getPageTitle(int position) {
            Drawable drawable = getContext().getResources().getDrawable(icons[position]);
            drawable.setBounds(0,0,48,48);
            ImageSpan imageSpan = new ImageSpan(drawable);
            SpannableString spannableString = new SpannableString(" ");
            spannableString.setSpan(imageSpan, 0,spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            return spannableString;
        }