Add Icons to SlidingTabLayout instead of Text

The key to this is to return a SpannableString, containing your icon in an ImageSpan, from your PagerAdapter’s getPageTitle(position) method: private int[] imageResId = { R.drawable.ic_tab_notifications, R.drawable.ic_tab_weather, R.drawable.ic_tab_calendar }; @Override public CharSequence getPageTitle(int position) { Drawable image = getResources().getDrawable(imageResId[position]); image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight()); SpannableString sb = new SpannableString(” “); ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM); … Read more

How do I implement swiping between tabs on Android?

NOTE: This is an excerpt from the Android Training class Implementing Effective Navigation. To implement this (in Android 3.0 or above), you can use a ViewPager in conjunction with the ActionBar tabs API. Upon observing the current page changing, select the corresponding tab. You can set up this behavior using an ViewPager.OnPageChangeListener in your activity’s … Read more

CoordinatorLayout inside another CoordinatorLayout

I know it’s an old question. But I searched a long time to include an CoordinatorLayout in a fragment, which is in another CoordinatorLayout. I modified the answer of dev.bmax a little bit to call both coordinator layouts and call the attached behaviors of both layouts. So here is my solution. @SuppressWarnings(“unused”) public class NestedCoordinatorLayout … Read more

View pager and fragment lifecycle

The FragmentPagerAdapter keeps additional fragments, besides the one shown, in resumed state. The solution is to implement a custom OnPageChangeListener and create a new method for when the fragment is shown. 1) Create LifecycleManager Interface The interface will have two methods and each ViewPager’s Fragment will implement it. These methods Are as follows: public interface … Read more