Returns a loaded custom font based on its identifier. - Android Graphics

Android examples for Graphics:Font

Description

Returns a loaded custom font based on its identifier.

Demo Code


//package com.java2s;
import android.content.Context;
import android.graphics.Typeface;

public class Main {
    public static final String[] fontPath = { "fonts/Blox2.ttf",
            "fonts/FolksInCube.ttf", "fonts/GautsMotelLowerLeft.ttf",
            "fonts/GautsMotelLowerRight.ttf",
            "fonts/GautsMotelUpperLeft.ttf",
            "fonts/GautsMotelUpperRight.ttf", "fonts/HighLevel.ttf",
            "fonts/Intaglio.ttf", "fonts/VintageOne.ttf" };
    public static boolean fontsLoaded = false;
    public static Typeface[] fonts = new Typeface[fontPath.length];

    /**//  w  ww.  j  a v  a  2  s. c  o  m
     * Returns a loaded custom font based on its identifier.
     *
     * @param context        - the current context
     * @param fontIdentifier - the identifier of the requested font
     * @return Typeface object of the requested font.
     */
    public static Typeface getTypeface(Context context, int fontIdentifier) {

        //If fonts aren't loaded, load them
        if (!fontsLoaded) {
            loadRobotoFonts(context);
        }

        //Return the typeface selected
        return fonts[fontIdentifier];
    }

    /**
     * Load all the fonts
     *
     * @param context - the current context
     */
    public static void loadRobotoFonts(Context context) {
        for (int i = 0; i < fontPath.length; i++) {
            fonts[i] = Typeface.createFromAsset(context.getAssets(),
                    fontPath[i]);
        }
        fontsLoaded = true;

    }
}

Related Tutorials