I'm implementing a SlidingTabLayout in my android application. What my query is that I'd like to implement icons in my Sliding Tabs instead of texts for navigation. I searched heavily on the internet for any such tutorial or sample but found none. I also searched a previous question on stackoverflow: Over Here - SlidingTabLayout with Icons. It was slightly informative but didn't really help me out.
To be clear. I require my tabs to consist of icons only. No text.
As I am new to this whole setup, I'd appreciate if you'd post proper code with an explanation. Thank you for your time and effort!
P.S. I also heard about the pagerslidingtabstrip by Andreaz Stuetz and wondered if that would be more suitable for the type of thing I'm going for...
Also: Here is what I would like my sliding tabs to look like. Check out the top of this image.
EDIT : NOW THAT LOLLIPOP (WITH MATERIAL DESIGN) HAS COME OUT. I HAVE SEEN A LOT OF APPS USING A NEW "ONLY ICON" SLIDING-TAB-LAYOUT
BELOW THEIR TOOLBAR (ANDROID 5.0 ACTION-BAR). IS THEIR ANY NEW WAY TO IMPLEMENT THIS?? THANKS ONCE AGAIN!
Extend your main activity from FragmentActivity. Also implement this class from ActionBar.TabListener as we are adding Tabs
// For Adding Tabs
Check here.
Your Adapter should implements
PagerSlidingTabStrip.IconTabProvider
.Then in
getPageIconResId
you can return id of image resource:Keep in mind that only image will be shown, text won't.
Ok.. there are many ways of implementing this change. This is the most quick and dirty one, just for the idea..
Substitue the method SlidingTabLayout.populateTabStrip() for this..
Create each layout this way LinearLayout > ImageView elements, src pointing to icon..
To customize SlidingTabLayout the way you want, you only need to modify the method populateTabStrip():
Your layout could be something like that:
The way you implement the getResourceArray() method is up to you. Here is how I did:
In the activity:
Be aware that in order to have access to R.layout.tab_layout*, you have to import yourpackage.R instead of android.R as it is by default in SlidingTabStrip.
I recently implemented this library called
ViewPagerAddons
. It includesSlidingTabLayoutColors
, which is exactly what you need. Simply let your pager adapter implementSlidingTabLayouColors.ImageProvider
interface, overridegetPageImage
method and return the image resource ids according to the page positions. Simple.Here's the link to the library (set up instructions included): https://bitbucket.org/enthusiast94/viewpageraddons/src