1

I'm trying to set a custom font to my game, but I really don't know how. The way I choose the font at the moment is using this method

 public void loadFont() { font = FontFactory.createStroke(activity.getFontManager(), activity.getTextureManager(), 256, 256, Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD), 50, true, Color.WHITE_ABGR_PACKED_INT, 2, Color.BLACK_ABGR_PACKED_INT); font.load(); } 

Where "Typeface.SANS_SERIF" is the font

If you know how I could load a font from the assets folder could you please help me? Thank you

**I am using AndEngine-GLES2-AnchorCenter

4 Answers 4

2

Add your yourfont.ttf or yourfont.otf into Assets in your Project and load custom font like

Typeface tf = null; tf = Typeface.createFromAsset(context.getAssets(), "Helvetica neue.ttf"); TextView text = (TextView) findViewById(R.id.yourtextview); tv.setTypeface(face); 
Sign up to request clarification or add additional context in comments.

Comments

2

I figured it out. Here's the code

public void loadFont() { font = FontFactory.createStroke(activity.getFontManager(), activity.getTextureManager(), 256, 256, Typeface.create(Typeface.createFromAsset(activity.getAssets(), "fonts/CUSTOMFONTNAME.ttf"), Typeface.BOLD), 50, true, Color.TRANSPARENT_ABGR_PACKED_INT, 2, Color.BLACK_ABGR_PACKED_INT); font.load(); 

1 Comment

that is great :) +1 for question and answer both than
0

In Andengine-GLES2 you can use custom font this way in onCreateResources():

 FontFactory.setAssetBasePath("font/"); final ITexture fontTexture = new BitmapTextureAtlas(activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR); this.mFont = FontFactory.createFromAsset(this.getFontManager(), fontTexture, activity.getAssets(), "your_font_name.ttf", 40, true, android.graphics.Color.BLACK); this.mFont.load(); 

You can use "Typeface.SANS_SERIF" font this way:

 this.mFont = FontFactory.create(this.getFontManager(), this.getTextureManager(), 256, 256, Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL), 32); this.mFont.load(); 

Comments

0

hi please do like this

 public void loadFont() { font = FontFactory.createStroke(activity.getFontManager(), activity.getTextureManager(), 256, 256, Typeface.create(Typeface.createFromAsset(activity.getAssets(), "fonts/CUSTOMFONTNAME.ttf"), Typeface.BOLD), 50, true, Color.TRANSPARENT_ABGR_PACKED_INT, 2, Color.BLACK_ABGR_PACKED_INT); font.load();} 

if there is any text view than

TextView text = (TextView) findViewById(R.id.custom_font); Typeface font = Typeface.createFromAsset(getAssets(), "yourfont.ttf"); text.setTypeface(font); 

beside this visit my blog http://upadhyayjiteshandroid.blogspot.in/2012/12/android-customfont.html and do it in this way

Programaticaaly you can do as follows, You can create subclass for each TextView, Button, etc. and apply custom font in the constructor:

public class BrandTextView extends TextView { public BrandTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public BrandTextView(Context context, AttributeSet attrs) { super(context, attrs); } public BrandTextView(Context context) { super(context); } public void setTypeface(Typeface tf, int style) { if (style == Typeface.BOLD) { super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/YourCustomFont_Bold.ttf")); } else { super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/YourCustomFont.ttf")); } } } 

Then just use that custom views instead of standard ones (i.e. BrandTextView instead of TextView).

<com.your.package.BrandTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="View with custom font"/> <com.your.package.BrandTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:text="View with custom font and bold typeface"/> 

use these all in yours main.xml and you can use them

another possible way can be as follows as well

FontHelper.applyFont(context, findViewById(R.id.activity_root), "fonts/YourCustomFont.ttf"); where applyFont method acn be written as follows public static void applyFont(final Context context, final View root, final String fontName) { try { if (root instanceof ViewGroup) { ViewGroup viewGroup = (ViewGroup) root; for (int i = 0; i < viewGroup.getChildCount(); i++) applyFont(context, viewGroup.getChildAt(i), fontName); } else if (root instanceof TextView) ((TextView) root).setTypeface(Typeface.createFromAsset(context.getAssets(), fontName)); } catch (Exception e) { Log.e(TAG, String.format("Error occured when trying to apply %s font for %s view", fontName, root)); e.printStackTrace(); } } 

please visit for more better understanding http://vision-apps.blogspot.in/2012/02/android-better-way-to-apply-custom-font.html

2 Comments

Parts of the answer seem to be directly copied from vision-apps.blogspot.com/2012/02/android-better-way-to-apply-custom-font.html - please add attribution when you're using other people's work.
@laalto i have added the attribution and the link upadhyayjiteshandroid.blogspot.in/2012/12/… is of my own blog as well

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.