[SOLVED] Android: Using Custom Fonts

Android comes with 3 fonts (Sans, Serif, Monospace) which can be accesed using android:typeface=”FONT_NAME”. But most of the times you would want to use your own fonts instead of the stock ones. Android provides an “assets” folder where you can put your TTF font files and access them from within the code.

There are two ways of doing this,

1. Assign the Typeface object to TextView,

2. Paint the Typeface on screen Canvas.

Method 1 – Assigning Typeface object to TextView

public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView bauhs = null,chiller=null,broadw=null,joker=null ;
        Button mag=null;

        setFont(bauhs, "fonts/BAUHS93.TTF", R.id.bauhs);

        setFont(broadw, "fonts/BROADW.TTF", R.id.broadw);

        setFont(chiller, "fonts/CHILLER.TTF", R.id.chiller);

        setFont(joker, "fonts/JOKERMAN.TTF", R.id.joker);

        setFont(mag, "fonts/MAGNETOB.TTF", R.id.magneto);

    }

    void setFont(TextView name, String path, int res)
    {
    	name=(TextView)findViewById(res);
        Typeface font = Typeface.createFromAsset(this.getAssets(), path); 

        name.setTypeface(font); 

    }

- “Typeface” is the class for handling fonts.

- The createFromAsset() method is used to specify the font path.

font object is assigned to the TextView name

Method 2 – Drawing the font on screen canvas

@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(new FontView(this));
    }

    private static class FontView extends View
    {
        private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        private Typeface mFace;

        public FontView(Context context)
        {
            super(context);

           // mFace = Typeface.createFromAsset(getContext().getAssets(),"fonts/MAGNETOB.TTF");

            mPaint.setTextSize(34);
            mPaint.setColor(Color.WHITE);
        }

        @Override
        protected void onDraw(Canvas canvas)
        {
            canvas.drawColor(Color.BLACK);

            drawFont(canvas, "fonts/BAUHS93.TTF", 50, "Bauhaus 93");

            drawFont(canvas, "fonts/BROADW.TTF", 150, "Broadway");

            drawFont(canvas, "fonts/CHILLER.TTF", 250, "Chiller");

            drawFont(canvas, "fonts/JOKERMAN.TTF", 350, "Jokerman");

        }

        void drawFont(Canvas canvas, String path, int y, String name)
        {
        	 mFace = Typeface.createFromAsset(getContext().getAssets(),path);

        	 mPaint.setTypeface(mFace);
             canvas.drawText(name, 30, y, mPaint);

        }
    }

- We need to extend the View class to access and override the onDraw() method.

- A Paint object is created which allows us to set the font size and color.

- The TypeFace object mFace is assigned to mPaint which is drawn on screen using the drawText().

drawText() takes 3 parameters, the text to be painted, its X-Y location and the Paint object.


Note: There is a difference between arial.ttf and ARIAL.TTF . If it ARIAL.TTF in your /assets folder and arial.ttf in your code, it will not work.

So if you have set the path correct but still wondering why the font is not being displayed, check this naming.

Source: Using Custom Fonts – ModMyGPhone Wiki.

Advertisement

7 Responses to [SOLVED] Android: Using Custom Fonts

  1. Rob says:

    Hi,
    Is there a way to specify a custom font from assets in the xml layouts or theme files?

    Maybe something like:

    
        @assets/fonts/customfont.ttf
    
  2. Rob says:

    Your comment system remove my xml example even though its in a pre block. lame.

  3. JazJ says:

    Thanks.
    But my requirement is to apply typeface/font on a soft keyboard.
    Like they do in
    http://betterandroid.wordpress.com/2009/05/24/introducing-better-keyboard-for-android/ ?

    Any way to do this??
    Please help me out on this one.

  4. Giovanni says:

    Thanks for the tip, really useful!

  5. kamal says:

    can we use the Arial typeface directly throw the xml file attribute.. ?

  6. Pingback: .::灵狼天::. » Blog Archive » 如何让 iOS 和 Android 支持自定义字体

  7. kamatchi says:

    thankyou for your answer.i got solution for my button font.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.