Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager - customizing title PagerTabStrip

I used title in ViewPager like on the screen below : just a text with a android.support.v4.view.PagerTabStrip

in OnPageChangeListener() I use method public CharSequence getPageTitle(int position) and it sets title

enter image description here

is there any way to customize that field to look like this :

enter image description here

may be some kind of custom adapter for title?

like image 661
Vlad Alexeev Avatar asked Feb 26 '14 14:02

Vlad Alexeev


1 Answers

You can easily add a icon/drawable to the PageTabStrip using SpannableString or SpannableStringBuilder.

For example, to display an icon before the text :

Drawable myDrawable; //Drawable you want to display

@Override
public CharSequence getPageTitle(int position) {

SpannableStringBuilder sb = new SpannableStringBuilder(" " + "Page #"+ position); // space added before text for convenience

myDrawable.setBounds(0, 0, myDrawable.getIntrinsicWidth(), myDrawable.getIntrinsicHeight()); 
ImageSpan span = new ImageSpan(myDrawable, ImageSpan.ALIGN_BASELINE); 
sb.setSpan(span, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

return sb;
}
like image 182
Bhavit S. Sengar Avatar answered Sep 27 '22 03:09

Bhavit S. Sengar