Bipin Bhandari
Bipin Bhandari

Reputation: 2692

Access a typeface once from asset and use it as a reference

I am trying to create a method that returns titleFont and contentFont typeface so that it would improve efficiency by just returning reference and reduce length of code. I know its easy but I am not getting the way. Can anyone help please. or any alternatives would be appreciated.. Sorry for bad english

Here is the method that will be running several times..

protected void onPostExecute(Article result) {
    super.onPostExecute(result);

    TextView txtTitle= (TextView) view.findViewById(R.id.title);
    txtTitle.setTypeface(titleFont);
    txtTitle.setText(result.getTitle());

    private Typeface titleFont=Typeface.createFromAsset(context.getAssets(),"fonts/InterstateCondMonoLgt.ttf");
    private Typeface contentFont=Typeface.createFromAsset(context.getAssets(),"fonts/InterstateLight.ttf");

    TextView txtMain= (TextView) view.findViewById(R.id.main);
    txtMain.setTypeface(contentFont);
    txtMain.setText(result.getContent());
}

Upvotes: 2

Views: 3172

Answers (4)

Darko Rodic
Darko Rodic

Reputation: 1020

You can make one utility class in your project with

private Typeface titleFont=Typeface.createFromAsset(context.getAssets(),"fonts/InterstateCondMonoLgt.ttf");
private Typeface contentFont=Typeface.createFromAsset(context.getAssets(),"fonts/InterstateLight.ttf");

and then simply write geter for it like

public Typeface getTitleFont() {
    return titleFont;
}

or what I like more, if you have inherited classes you put in your base class like:

public static Typeface titleFont;

and then

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    titleFont=Typeface.createFromAsset(context.getAssets(),"fonts/InterstateCondMonoLgt.ttf");
}

this way you will always have fonts just call textView.setTypeface(titleFont); anywhere you need

Upvotes: 1

Diego Palomar
Diego Palomar

Reputation: 7061

I hope this help you ;)

FontUtil class

public class FontUtil {

    private static Typeface mTitleFont;
    private static Typeface mContentFont;

    public static enum FontType {

        TITLE_FONT {
            public String toString() {
                return "fonts/InterstateCondMonoLgt.ttf";
            }
        },

        CONTENT_FONT {
            public String toString() {
                return "fonts/InterstateLight.ttf";
            }
        }
    }

    /**
     * @return Typeface Instance with the font passed as parameter
     */
    public static Typeface getTypeface(Context context, String typefaceName) {
        Typeface typeFace = null;

        try {
            if (typefaceName.equals(FontType.TITLE_FONT.toString())) {
                if (mTitleFont == null) {
                    mTitleFont = Typeface.createFromAsset(context.getAssets(), typefaceName);
                }
                typeFace = mTitleFont;
            } else if (typefaceName.equals(FontType.CONTENT_FONT.toString())) {
                if (mContentFont == null) {
                    mContentFont = Typeface.createFromAsset(context.getAssets(), typefaceName);
                }
                typeFace = mContentFont;
            } 
        } catch (Exception ex) {
            typeFace = Typeface.DEFAULT;
        }

        return typeFace;
    }

    /**
     * @return Typeface Instance with the font passed as parameter
     */
    public static Typeface getTypeface(Context context, FontType typefaceName) {
        return getTypeface(context, typefaceName.toString());
    }
}

Client

protected void onPostExecute(Article result) {
    super.onPostExecute(result);

    TextView txtTitle= (TextView) view.findViewById(R.id.title);
    txtTitle.setTypeface( FontUtil.getTypeface(mContext, FontType.TITLE_FONT) );
    txtTitle.setText(result.getTitle());

    TextView txtMain= (TextView) view.findViewById(R.id.main);
    txtMain.setTypeface( FontUtil.getTypeface(mContext, FontType.CONTENT_FONT) );
    txtMain.setText(result.getContent());
}

Upvotes: 6

Harish Godara
Harish Godara

Reputation: 2386

Create some util class and put all code there like :

public class Utils
{
    public static Typeface getTitleFont()
    {
        return Typeface.createFromAsset(getApplicationContext().getAssets(),"fonts/InterstateCondMonoLgt.ttf");
    }
}

And use like this :

TextView txtMain= (TextView) view.findViewById(R.id.main);
txtMain.setTypeface(your_package.Utils.getTitleFont());

Upvotes: 0

Ritaban
Ritaban

Reputation: 773

All the variables created inside a particular method is private on its own . You cant give access modifiers to any variables declared inside a method. It will give you compilation error. I will suggest you to declare this typefaces variable as class level variable and initialize them inside the constructor of the ASYNCTASK. Otherwise every time your onPostExecute() will get called , every time it will create a typeface which may cause memory overhead at a later point of time.

Upvotes: 0

Related Questions