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.
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>
VimalTuts Code Junction 
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.
Great,
You have just said what I am doing.
This morning I have completed the advance map with gprs alert system. I will post it asap.
Keep in touch
Sir,
i m new in android ..so i just developed one program in android ..and i dnt understand how to learn android ..plz help me out ..plz
Hai Preetiphogat,
Sorry for the late response, I have been busy for the last two weeks as I am searching for a Job
So I didn’t have enough time to post new tutorials and take a look at comments.
Do you still have problem with learning Android?
If you want me to help you out? Just leave reply for the following questions.
1) Did you successfully install android sdk ? http://vimaltuts.com/android/android-tutorial-for-beginners
2) Did you take a look at this tutorial? — http://vimaltuts.com/android/first-android-program
3) Or In what way Can I help you?
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
dear sir,
How to open or view your project, blog is locked
you have some solution, ie urgent
Thanks
Sapan
Mailed You.
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?
Ok, I am going to unlock all the post. But keep sharing and add me to your circle.