2

I'm trying to set Font Awesome Arrows as the 'previous' and 'next' instead of my page titles. How do I accomplish this? I know how to manually add in the words, now How can I change that to the referenced arrows defined in my strings.xml?

        Typeface font = Typeface.createFromAsset( getAssets(), "fonts/fontawesome-webfont.ttf" );
        @Override
        public String getPageTitle(int position) {

                if (position == current_position - 1) {
                     String title = "Previous";
                     return title;
                } else if (position == current_position + 1) {
                     String title = "Next";
                     return title;
                }
            return title;   
        }

For future readers, this is what worked based on the answer provided:

 final Typeface font = Typeface.createFromAsset(getAssets(), "fonts/fontawesome-webfont.ttf" );
           @Override
            public CharSequence  getPageTitle(int position) {
             String title = "";
              if (position == current_position - 1) {
                title = "\uf137";
                SpannableStringBuilder styled = new SpannableStringBuilder(title);
                styled.setSpan(new CustomTypefaceSpan("", font), 0, title.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                //Previous
                return styled;

            } else if (position == current_position + 1) {
                //Next
                title = "\uf138";
                SpannableStringBuilder styled = new SpannableStringBuilder(title); 
                styled.setSpan(new CustomTypefaceSpan("",font), 0, title.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                return styled;
            }
Petro
  • 3,484
  • 3
  • 32
  • 59

1 Answers1

1

In order to apply any custom styling directly to a text sequence (rather than setting it on the view), you can wrap the string in a Span. Unfortunately, the framework does not have a built-in span object that accepts a Typeface object (TypefaceSpan accepts a font family instead).

However, there are many examples (such as this one) for creating a simple subclass that will apply your custom type face.

Applying it would look something like this:

@Override
public CharSequence getPageTitle(int position) {
    Typeface font = Typeface.createFromAsset( getAssets(), "fontawesome-webfont.ttf" );
    String title = "";

    if (position == current_position - 1) {
        title = "\uf137";
    } else if (position == current_position + 1) {
        title = "\uf138";
    }

    SpannableString styled = new SpannableString(title);
    styled.setSpan(new CustomTypefaceSpan(font), 0, title.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

    return styled;
}
Community
  • 1
  • 1
devunwired
  • 62,780
  • 12
  • 127
  • 139
  • Thanks! I will check this out when i get home – Petro Feb 24 '15 at 04:09
  • Hi, this approach did not return anything. – Petro Feb 25 '15 at 03:52
  • Can you perhaps be a bit more descriptive about what didn't work? If you are seeing no text, did you try using a different span (like `ScaleXSpan`, or something simple) to be sure it isn't the span definition that is incorrect? – devunwired Feb 25 '15 at 04:27
  • Please see my updated question, span is not working for me it's still showing the actual text, not the font-awesome styled text; – Petro Feb 25 '15 at 15:23
  • You're using web notation to try and get a unicode value into the string. Android doesn't understand that. Your string values need to be `\uf137` and `\uf138` instead. The `\u` prefix denotes that this is a hex unicode value for the character. – devunwired Feb 25 '15 at 16:46
  • Also...you may have noticed that linked question has another answer with a better span definition than what you are using. The non-accepted version doesn't take a font family string, which is redundant anyway. – devunwired Feb 25 '15 at 16:47
  • @Devunwired where should i override this getPageTitle(int position) {} function?? – KJEjava48 Mar 08 '17 at 11:25