Android Custom Font Example

Android Custom Font Example

  • There will be times when you want a various font, probably when you are build word processor application, or most likely simply because it appears to be pleasant.
  • This is really simple to accomplish. Simply place your TrueType (TTF) or even OpenType file (OTF) fonts in to the assets folder within your project. After that you can load it right into a
    Typeface class, assign it into a Paint class or even TextView.
Loading a Custom Font from Assets
TextView tv = (TextView) findViewById(R.id.myfontfile);
		Typeface face = Typeface.createFromAsset(getAssets(),"fonts/CHOCD.otf");
		tv.setTypeface(face);

Our desire font is loaded into a Typeface object which is shown in the above snippet.
So What’s an Asset?

Android has its own very particular ideas about what kinds of files can go where, as well as makes use of naming conventions to deal with various system configuration settings. An asset could be any kind of file, which includes subfolders.

You actually gain access to all of them utilizing an AssetManager. This can be simple to find, by calling the actual Context’s getAssets() method.

The most frequent method to make use of AssetManager requires you to call the actual open() method to returning an InputFileStream. As a matter of fact, a good number of Android classes have got techniques to load straight from an asset. For all those classes which don’t wish to perform straight along with InputStreams or even AssetManager, you are able to obtain a FileDescriptor.

FileDescriptor fd = getAssets().openFd("myfile.txt").getFileDescriptor();


It is possible to unpack it right into a temporary file and also use that.
Creating Layout for Custom Font
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1" >
    <TableRow>
        <TextView
            android:id="@+id/myfontfile"
            android:text="@string/vimaltuts"
            android:textColor="@color/aqua"
            android:textSize="80sp" />
    </TableRow>
    <TableRow>
        <TextView
            android:id="@+id/myfontfile2"
            android:text="@string/vimaltuts"
            android:textColor="@color/aqua"
            android:textSize="60sp" />
    </TableRow>
    <TableRow>
        <TextView
            android:id="@+id/myfontfile3"
            android:text="@string/vimaltuts"
            android:textColor="@color/aqua"
            android:textSize="120sp" />
    </TableRow>
    <TableRow>
        <TextView
            android:id="@+id/myfontfile4"
            android:text="@string/vimaltuts"
            android:textColor="@color/aqua"
            android:textSize="40sp" />
    </TableRow>
    <TableRow android:id="@+id/text_hide">
        <TextView
            android:id="@+id/myfontfile5"
            android:text="@string/vimaltuts"
            android:textColor="@color/aqua"
            android:textSize="60sp" />
    </TableRow>
</TableLayout>
Java Code To Configure the Custom Font
src/CustomFontExampleActivity.java
package com.vimaltuts.android.customfont;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.TextView;
import java.io.File;
public class CustomFontExampleActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.main);
		TextView tv = (TextView) findViewById(R.id.myfontfile);
		Typeface face = Typeface.createFromAsset(getAssets(),"fonts/CHOCD.otf");
		tv.setTypeface(face);
		tv = (TextView) findViewById(R.id.myfontfile2);
		face = Typeface.createFromAsset(getAssets(),"fonts/ABADDON.TTF");
		tv.setTypeface(face);
		tv = (TextView) findViewById(R.id.myfontfile3);
		face = Typeface.createFromAsset(getAssets(),"fonts/ANGEL.otf");
		tv.setTypeface(face);
		tv = (TextView) findViewById(R.id.myfontfile4);
		face = Typeface.createFromAsset(getAssets(),"fonts/NorthernTerritories.ttf");
		tv.setTypeface(face);
		tv = (TextView) findViewById(R.id.myfontfile5);
		face = Typeface.createFromAsset(getAssets(),"fonts/WoodWud.ttf");
		tv.setTypeface(face);
	}
}

It is possible to load a font out of a local data file and use it. The advantage is that you simply can easily personalize your own fonts right after your application has been distributed. However, you need to in some way organise to obtain the font on to these devices. And you can aquire a Typeface through createFromAsset(), you may get a Typeface through createFromFile(). If it is located, we utilize it. In any other case, we hide out that row.

 <TableRow android:id="@+id/text_hide">
        <TextView
            android:id="@+id/myfontfile5"
            android:gravity="center_horizontal"
            android:text="@string/vimaltuts"
            android:textColor="@color/aqua"
            android:textSize="60sp" />
    </TableRow>
Output Screen of the Android Custom Font Example


So far we have play with the Android Action Custom Font Example through this Android Tutorial for Beginners : Android Custom Font Example Tutorial. On the next tutorial we gonna see about the Android Notification Example

9 comments

  1. It is very amazing web site.i am very appreciate with you.But l request to you please provide advance android concepts and whole project development tutorials also.advance concepts in the sense Google map,email sending etc.
    Thank You.

  2. hello vimal. i really appreciate your work.actually what i was searching here i could not. i just want to apply green droid library in my project for action bar.but when i am adding the jar i dont know y but getting problem so if u plz post the whole concept for how we ll use green droid lib in project which will show us action bar.thanks in advance

  3. dear sir,
    How to open or view your project, blog is locked
    you have some solution, ie urgent

    Thanks
    Sapan

  4. I started to learn android from this october 15,2012. I want advices, innstructions, books, examples of code from u. ur blog is locked. how to open?

Leave a Reply

Scroll To Top

Foolow Me on Google Plus

VimalTuts on Twitter
62 people follow VimalTuts
MageshKeiambgvLukus_Q7ravon30silly_piubemafoovedpawarmohammed