mytechead

Android & Wordpress Help center

Add custom font type in Android

Adding custom font type other than the default – ‘sans’, ‘serif’, ‘monospace’ & ‘normal’ font types is pretty easier than one can think of!

We can start by creating an xml file to place our TextView :

<?xml version=”1.0″ encoding=”utf-8″?>

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>

<TextView
android:id=”@+id/custom_font_text”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”This is the Chantelli Antiqua font.”
/>

</LinearLayout>

Here we have a TextView with id ‘custom_font_text’, this has been added as we need to add custom font to this TextView programmatically. Now here is what has to be done next –

1) Download a custom font of your choice- the file will be of the format ttf or otf. For instance here we have used ‘HelveticaNeue-Roman.otf’.

2) Put this file in assets folder of your current project.

3) Use the below mentioned code to use this font in your activity –

TextView myText = (TextView) findViewById(R.id.custom_font_text);
Typeface font = Typeface.createFromAsset(getAssets(), “.HelveticaNeue-Roman.otf”);
myText.setTypeface(font);

The above code will set the font typeface for this particular TextView to HelveticaNeue-Roman.

Advertisements

Single Post Navigation

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: