user3740505
user3740505

Reputation: 141

Custom font in Android app and ActionBar

I've been busting my head all afternoon on implementing a custom font in my Android app. First, I had troubles with a custom font in my navigation drawer. That doesn't seem to work. So I decided to try the Actionbar title, but I'm having equal problems.

I found this website http://www.tristanwaddington.com/2013/03/styling-the-android-action-bar-with-a-custom-font/ and decided to try it.

I created a custom java file with the code below:

package vimen.vimenlogin;

import android.content.Context;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.MetricAffectingSpan;
import android.util.LruCache;

public class TypefaceSpan extends MetricAffectingSpan {
    /** An <code>LruCache</code> for previously loaded typefaces. */
    private static LruCache<String, Typeface> sTypefaceCache =
    new LruCache<String, Typeface>(12);

    private Typeface mTypeface;

    /**
     * Load the {@link Typeface} and apply to a {@link}.
     */
    public TypefaceSpan(Context context, String typefaceName) {
        mTypeface = sTypefaceCache.get(typefaceName);

        if (mTypeface == null) {
            mTypeface = Typeface.createFromAsset(context.getApplicationContext()
                .getAssets(), String.format("fonts/%s", typefaceName));

            // Cache the loaded Typeface
            sTypefaceCache.put(typefaceName, mTypeface);
        }
    }

    @Override
    public void updateMeasureState(TextPaint p) {
        p.setTypeface(mTypeface);

        // Note: This flag is required for proper typeface rendering
        p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }

    @Override
    public void updateDrawState(TextPaint tp) {
        tp.setTypeface(mTypeface);

        // Note: This flag is required for proper typeface rendering
        tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }
}

Afther that, I put the method in my main.java file:

public void restoreActionBar()
{
    actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    actionBar.setDisplayShowTitleEnabled(true);

    SpannableString s = new SpannableString("testing");
    s.setSpan(new TypefaceSpan(this, "fonts/Raleway-Bold.ttf"), 0, s.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    // Update the action bar title with the TypefaceSpan instance
    actionBar.setTitle(s);
    //actionBar.setTitle(mTitle);
}

However, I'm getting this error Caused by: java.lang.RuntimeException: native typeface cannot be made coupled with a crashing app.

Upvotes: 2

Views: 380

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007464

TypefaceSpan does not take a font path. It only works with the built-in font families. It is impossible to tell from the code snippets as they stand whether you are using your TypefaceSpan or the built-in TypefaceSpan.

(pro tip: use a different class name for yours than TypefaceSpan)

The error message in general means either Android cannot find the font file or cannot interpret it for some reason. Given your code, your font file would need to be:

assets/fonts/fonts/Raleway-Bold.ttf

in your project, as you have fonts/ both in your TypefaceSpan and in your use of that TypefaceSpan. If the font file is not there, either move it or fix up your code to reference its actual location.

Upvotes: 3

Related Questions