Example usage for android.graphics Typeface createFromAsset

List of usage examples for android.graphics Typeface createFromAsset

Introduction

In this page you can find the example usage for android.graphics Typeface createFromAsset.

Prototype

public static Typeface createFromAsset(AssetManager mgr, String path) 

Source Link

Document

Create a new typeface from the specified font data.

Usage

From source file:Main.java

/**
 * Sets Roboto Light font for a given text view.
 * @param textView//from w w w .  j  a v a2  s  .c o m
 * @param assetManager
 */
public static void setRobotoLight(TextView textView, AssetManager assetManager) {
    textView.setTypeface(Typeface.createFromAsset(assetManager, "font/Roboto-Light.ttf"));
}

From source file:Main.java

/**
 * Sets Roboto Light Italic font for a given text view.
 * @param textView/*from  ww w . j a va2  s .c o  m*/
 * @param assetManager
 */
public static void setRobotoLightItalic(TextView textView, AssetManager assetManager) {
    textView.setTypeface(Typeface.createFromAsset(assetManager, "font/Roboto-LightItalic.ttf"));
}

From source file:Main.java

public static Typeface get(Context c, String name) {
    synchronized (cache) {
        if (!cache.containsKey(name)) {
            try {
                Typeface t = Typeface.createFromAsset(c.getAssets(), String.format("fonts/%s", name));
                cache.put(name, t);/*from www . ja  v  a2 s.  c o  m*/
                return t;
            } catch (RuntimeException e) {
                return null;
            }
        }
        return cache.get(name);
    }
}

From source file:Main.java

/**
 * Sets the font on a particular TextView
 * @param assets AssetManager/* w  ww  . j a va  2  s. c  o  m*/
 * @param tv TextView the text to apply the font to
 */
public static void setTitleFont(AssetManager assets, TextView tv) {
    Typeface tf = Typeface.createFromAsset(assets, TITLE_FONT);
    tv.setTypeface(tf);
}

From source file:Main.java

public static Typeface get(Context context, String font) {
    synchronized (sTypefaceCache) {
        if (!sTypefaceCache.containsKey(font)) {
            Typeface tf = Typeface.createFromAsset(context.getApplicationContext().getAssets(), font + ".ttf");
            sTypefaceCache.put(font, tf);
        }//ww w. ja va 2s  .  c  o  m
        return sTypefaceCache.get(font);
    }
}

From source file:Main.java

/**
 * //from   w  ww.  j  ava2  s .  co m
 * @param context
 * @return
 */
public static Typeface getFAFont(Context context) {
    Typeface tf = null;
    try {
        tf = Typeface.createFromAsset(context.getAssets(), font_awesome);
    } catch (Exception e) {
        Log.e(TAG, "Failed to load FontAwesome Font");
        Toast.makeText(context, "Failed to load FontAwesome Font", Toast.LENGTH_SHORT).show();
    }
    return tf;
}

From source file:Main.java

public static void createFonts(Context context, String path) {
    fonts = new HashMap<>();
    try {//from w ww  .  j  a  v  a 2 s .  c  o  m
        String[] listOfFonts = listAssetFiles(path, context);
        for (String s : listOfFonts) {
            Typeface typeface = Typeface.createFromAsset(context.getAssets(), path + "/" + s);
            fonts.put(s.substring(0, s.lastIndexOf(".")), typeface);
            Log.d("PKFontUtils", "fonts" + s.substring(0, s.lastIndexOf(".")));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static Typeface createFromAsset(final AssetManager assets, final String fontname) {
    Typeface result = null;/*from   w  ww. j  ava  2  s  .c o  m*/
    SoftReference<Typeface> cachedFont = getFromCache(fontname);

    if (null != cachedFont && cachedFont.get() != null) {
        result = cachedFont.get();
    } else {
        result = Typeface.createFromAsset(assets, fontname);
        putIntoCache(fontname, result);
    }

    return result;
}

From source file:Main.java

public static void setFont(TextView t, String font) {
    Typeface type = Typeface.createFromAsset(t.getContext().getAssets(), font);
    t.setTypeface(type);
}

From source file:Main.java

/**
 * Using reflection to override default typeface
 * NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN
 * @param context to work with assets/*from w w w  . j ava2  s.com*/
 * @param defaultFontNameToOverride for example "monospace"
 * @param customFontFileNameInAssets file name of the font from assets
 */
public static void overrideFont(Context context, String defaultFontNameToOverride,
        String customFontFileNameInAssets) {
    try {
        final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(),
                customFontFileNameInAssets);

        final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
        defaultFontTypefaceField.setAccessible(true);
        defaultFontTypefaceField.set(null, customFontTypeface);
    } catch (Exception e) {
    }
}