Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.content.Context;
import android.graphics.Typeface;

import java.util.HashMap;
import java.util.Map;

public class Main {
    private static Map<String, Typeface> typefaceCache = new HashMap<String, Typeface>();

    /**
     * Creates Roboto typeface and puts it into cache
     */
    private static Typeface getRobotoTypeface(Context context, String fontType, Typeface typeface) {
        String fontPath = "fonts/" + getFontName(fontType, typeface) + ".ttf";

        if (!typefaceCache.containsKey(fontType)) {
            typefaceCache.put(fontType, Typeface.createFromAsset(context.getAssets(), fontPath));
        }

        return typefaceCache.get(fontType);
    }

    private static String getFontName(String fontType, Typeface typeface) {
        if (fontType == null)
            if (typeface == null)
                return "Roboto-Regular";
            else if (typeface.isBold() && typeface.isItalic())
                return "Roboto-BoldItalic";
            else if (typeface.isBold())
                return "Roboto-Bold";
            else if (typeface.isItalic())
                return "Roboto-Italic";
            else
                return "Roboto-Regular";
        else if (fontType.equals("roboto-light"))
            if (typeface == null)
                return "Roboto-Light";
            else if (typeface.isItalic())
                return "Roboto-LightItalic";
            else
                return "Roboto-Light";
        else if (fontType.equals("roboto-condensed"))
            if (typeface == null)
                return "RobotoCondensed-Regular";
            else if (typeface.isBold() && typeface.isItalic())
                return "RobotoCondensed-BoldItalic";
            else if (typeface.isBold())
                return "RobotoCondensed-Bold";
            else if (typeface.isItalic())
                return "RobotoCondensed-Italic";
            else
                return "RobotoCondensed-Regular";
        else if (fontType.equals("roboto-slab"))
            if (typeface == null)
                return "RobotoSlab-Regular";
            else if (typeface.isBold())
                return "RobotoSlab-Bold";
            else
                return "RobotoSlab-Regular";
        else if (fontType.equals("roboto-slab-light"))
            return "RobotoSlab-Light";
        else if (fontType.equals("roboto-slab-thin"))
            return "RobotoSlab-Thin";
        else if (fontType.equals("roboto-condensed-light"))
            if (typeface == null)
                return "RobotoCondensed-Light";
            else if (typeface.isItalic())
                return "RobotoCondensed-LightItalic";
            else
                return "RobotoCondensed-Light";
        else if (fontType.equals("roboto-thin"))
            if (typeface == null)
                return "Roboto-Thin";
            else if (typeface.isItalic())
                return "Roboto-ThinItalic";
            else
                return "Roboto-Thin";
        else if (fontType.equals("roboto-medium"))
            if (typeface == null)
                return "Roboto-Medium";
            else if (typeface.isItalic())
                return "Roboto-MediumItalic";
            else
                return "Roboto-Medium";
        else if (typeface == null)
            return "Roboto-Regular";
        else if (typeface.isBold() && typeface.isItalic())
            return "Roboto-BoldItalic";
        else if (typeface.isBold())
            return "Roboto-Bold";
        else if (typeface.isItalic())
            return "Roboto-Italic";
        else
            return "Roboto-Regular";
    }
}