I know how to use string as titles in View Pager, but now I want to integrate font awesome into it. I don't know how to use custom type faces inside View Pager. Here is my viewPagerAdapter
code:
package com.example.skmishra.customiconsviewpager;
import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
public class ViewPagerAdapter extends FragmentStatePagerAdapter implements ActionBar.TabListener , ViewPager.OnPageChangeListener {
String Titles[]; // This will Store the Titles of the Tabs which are Going to be passed when ViewPagerAdapter is created
int NumbOfTabs; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created
private final ActionBar mActionBar;
Context context;
ViewPager mPager;
Typeface font;
// Build a Constructor and assign the passed Values to appropriate values in the class
public ViewPagerAdapter(FragmentManager fm, String mTitles[], int mNumbOfTabsumb, ActionBar mActionBar,Context context,ViewPager pager) {
super(fm);
this.Titles = mTitles;
this.NumbOfTabs = mNumbOfTabsumb;
this.context=context;
this.mActionBar = mActionBar;
this.mPager=pager;
}
//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {
if(position==0)
{
fragment_tab_bio tab1=new fragment_tab_bio();
return tab1;
}
else if(position==1)
{
fragment_tab_movies tab2=new fragment_tab_movies();
return tab2;
}
else
{
fragment_tab_funStuff tab3=new fragment_tab_funStuff();
return tab3;
}
}
@Override
public CharSequence getPageTitle(int position) {
Typeface font = Typeface.createFromAsset( context.getAssets(), "fontawesome-webfont.ttf" );
String title = "";
title = Titles[position];
SpannableString styled = new SpannableString(title);
styled.setSpan(new CustomTypefaceSpan("",font), 0, title.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
return styled;
}
// This method return the Number of tabs for the tabs Strip
@Override
public int getCount() {
return NumbOfTabs;
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
Custom Typeface Span Class
package com.example.skmishra.customiconsviewpager;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.TypefaceSpan;
public class CustomTypefaceSpan extends TypefaceSpan {
private final Typeface newType;
public CustomTypefaceSpan(String family, Typeface type) {
super(family);
newType = type;
}
@Override
public void updateDrawState(TextPaint ds) {
applyCustomTypeFace(ds, newType);
}
@Override
public void updateMeasureState(TextPaint paint) {
applyCustomTypeFace(paint, newType);
}
private static void applyCustomTypeFace(Paint paint, Typeface tf) {
int oldStyle;
Typeface old = paint.getTypeface();
if (old == null) {
oldStyle = 0;
} else {
oldStyle = old.getStyle();
}
int fake = oldStyle & ~tf.getStyle();
if ((fake & Typeface.BOLD) != 0) {
paint.setFakeBoldText(true);
}
if ((fake & Typeface.ITALIC) != 0) {
paint.setTextSkewX(-0.25f);
}
paint.setTypeface(tf);
}
}
Well the answer lies in the slidingTabLayout.java file by Google . In that file simply go to populataTabStrip function . In that function , look for setText(getPageTitle (position)) , just before that use the setTypeface function and your Typeface object as parameter .and you are done