How To Use Custom Fonts In Android

In android , you can define your own custom fonts for the strings in your application. You just need to download the required font from the internet, and then place it in assets/fonts folder.

After putting fonts in the assets folder under fonts folder, you can access it in your java code through Typeface class. First we need to get the reference of the text view in the code.

 

TextView tv = (TextView)findViewById(R.id.view1);
[/code]

The next thing you need to do is to call static method of Typeface class createFromAsset() to get your custom font from assets.


Typeface customfonttype = Typeface.createFromAsset(getAssets(), "fonts/font fontfile.ttf");

 

The last thing you need to do is to set this custom font object to your TextView Typeface property. You need to call setTypeface() method to do that.

 

tv.setTypeface(customfonttype);

 

 

In below example we will be demonstrating the use of Typeface to handle CustomFont. This will create a basic application that displays a custom font that you specified in the fonts file.

Create and Write following in main activity file src/MainActivity.java.

 

package com.customfonts;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView)findViewById(R.id.view1);
Typeface customfonttype = Typeface.createFromAsset(getAssets(),
"fonts/Erika fontfile.ttf");
tv.setTypeface(customfonttype);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

 

Write following into xml res/main.xml.

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<TextView
android:id="@+id/view1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="70dip"
android:text="@string/hello_world" />
</LinearLayout>

 

 

Write following into res/string.xml.

 

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">CustomFonts</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello</string>

</resources>

 

Write following into AndroidManifest.xml file.

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.customfonts"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.customfonts.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

 

 

Running this on emulator will display following Emulator window:

customfont1

You can see that the text that appeared on the AVD has not a default android font, rather it has the custom font that you specified in the fonts folder.