0

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);
    }
 }
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Sarthak Mishra
  • 1,033
  • 1
  • 11
  • 27
  • So what's happening with this code? How are the results different from what you want/expect? By the way, don't load font awesome for every title. Just load it once (into the `font` field, which you've declared but never use). – Ted Hopp Dec 15 '15 at 19:34
  • I can't seem to set the type face to the spannable – Sarthak Mishra Dec 15 '15 at 20:37
  • Whose `CustomTypefaceSpan` class are you using? – Ted Hopp Dec 15 '15 at 20:47
  • Please check my updated question – Sarthak Mishra Dec 16 '15 at 01:06
  • 1
    First, please explain what happens with your code; it's not very informative to say _"I can't seem to set the type face to the spannable"_. Why not? Do you get a compile error? A runtime error? Or does it simply not do anything? Second, the `CustomTypefaceSpan` class you are using looks very suspicious to me; I'd suggest that you instead use the one posted in [this answer](http://stackoverflow.com/a/17961854/535871). – Ted Hopp Dec 16 '15 at 03:27
  • It simply doesn't do anything . And I have used the class used in the answer you have linked , but without any success. – Sarthak Mishra Dec 16 '15 at 03:46
  • Could you please provide an answer with some code in it. – Sarthak Mishra Dec 16 '15 at 03:46

1 Answers1

0

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

Sarthak Mishra
  • 1,033
  • 1
  • 11
  • 27