How To Use Custom and SDK Fonts in Android ?

In this post i am included the simple steps to change font in android. The methods included to use default and custom fonts.
How To Use Available SDK fonts in Android?
Method 1 [ Using XML ]
1. Use android:typeface property in your view ie. Button,EditText,TextView etc. Where you can select any of the system font available in the SDK ie. Droid sans , Droid Sans Mono and Droid Serif.
<textview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="20sp" android:typeface="serif" / >
Method 2 [ Using Java ]
Call setTypeFace() method on the Views object eg TextView , EditText , Button, etc.
TextView textView = (TextView) findViewById(R.id.TextViewId); textView.setTypeface(Typeface.SERIF);
How To Use Custom fonts in Android?
Method [ Using Java ]
This also use the setTypeFace() method but need some additional steps.
1. Create a folder named fonts in assets folder and copy all fonts it to it. ( You can use any folder instead of fonts )
2. In Activity Java file Create TypeFace object and load font face from asset.
Typeface tf = Typeface.createFromAsset(getAssets(), "PATH_TO_FONT");
3. Finally set the created TypeFace to the view.
textView.setTypeface(typeface_object);
Here is the example code:
TextView textView = (TextView) findViewById(R.id.TextViewId); // Loading font from assets Typeface tfIron=Typeface.createFromAsset(getAssets(), "fonts/Iron.ttf"); // applying to the view textView.setTypeface(tfIron);
That's it...
0 comments: