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!
Check this solution link androidhive about material design tabs /viewpager
A very good website about android solutions. Hope you will find a solution.
The key to this is to return a SpannableString, containing your icon in an ImageSpan, from your PagerAdapter's getPageTitle(position) method:
Normally this be would be enough, but the default tab created by SlidingTabLayout makes a call to
TextView#setAllCaps(true)
which effectively disables all ImageSpans, so you'll have to use a custom tab view instead:res/layout/custom_tab.xml
and where ever you setup/bind to your ViewPager:
(make sure to call
setCustomTabView
beforesetViewPager
)I solved same problem, but also with changing drawable on selected tab.
Create your drawables for tabs, with two states. first_tab_drawable.xml, second_tab_drawable.xml, third_tab_drawable.xml:
Create your own pager adapter, extends from PagerAdapter:
Change code of SlidingTabLayout:
And make really selected TextView title also in SlidingTabLayout in InternalViewPagerListener:
Hope it will be helpful for somebody.
I know that this thread if fairly old, but i wanted to share my solution to this problem nevertheless, because it might be helpful for some. It works quite nice, even for different drawables depending on the selected state.
First of i did define two array inside the
SlidingTabLayout
class (this could easily be outsourced to another class):Now we alter the
populateTabStrip()
method inside theSlidingTabLayout
:To update the image according to which tab is selected we add some lines to the
onPageSelected
methodFinally we need to add a custom Tab View:
Like this one
This solution is far from perfect, but works for my needs. Maybe i could help someone with this.
I followed Jeremy's answer and its working fine. AFAIK there is no new way of doing this. Most apps are now removing the toolbar altogether and adding a custom slidingtabstrip recommended by google.
If you have any specific questions let me know. iv got my app looking a lot like the screenshot you have posted. I feel this material design update was focusing more on enabling custom animations and effects.
Just returning
null
from thegetPageTitle()
method did it for me: