How to use Custom Font In Android Applications

If you are new to Android development and wondering how to use custom font, here is a quick tip to use custom font in your Android application.

All you have to do is copy the font file ie with extension “.otf” (Open Type Font) or “.ttf” (True Type Font).

After loading the font file the next step is to set the font in you application using Typeface.createFromAsset(getAssets(),“fontname.otf”) method.

Typeface font = Typeface.createFromAsset(getAssets(), "FONTFILE.otf");

To use the loaded custom font, use setTypeface method on the widget you want to apply, for example, to use it on TextView, use textViewObj.setTypeface(customfont).

TextView myTextView = (TextView) findViewById(R.id.tv_mytext);

myTextView.setTypeface(font);

Every Android device comes with a collection of standard fonts: Droid Sans, Droid Sans Mono and Droid Serif. They were designed to be optimal for mobile displays.

Following examples shows how to use the style for textView

The attribute used is android:typeface

<TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="This is a 'sans' demo!"
 android:typeface="sans"
 />
 <TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="This is a 'serif' demo!"
 android:typeface="serif"
 />
 <TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="This is a 'monospace' demo!"
 android:typeface="monospace"
 />



Another Attribute is pretty interesting ie the text shadow

You can use three different attributes to customize the appearance of your text shadow:

  • android:shadowColor Shadow color in the same format as textColor.
  • android:shadowRadius Radius of the shadow specified as a floating point number.
  • android:shadowDx The shadow’s horizontal offset specified as a floating point number.
  • android:shadowDy The shadow’s vertical offset specified as a floating point number.

 

How to Use

<TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="A light blue shadow."
 android:shadowColor="#00ccff"
 android:shadowRadius="1.5"
 android:shadowDx="1"
 android:shadowDy="1"
 />

Preview

Advertisements

What you think ? 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