Monday, December 2, 2013

Custom Fonts TypeFace in Android Example

No comments :
Hi,

This is an Example for Create Custom Fonts or External fonts in Android


Although it would have been appreciated there is no easy way to set a default custom font in your Android application. Android as of Ice Cream Sandwich comes with a default typeface called Roboto, which in my opinion is a very good and appealing typeface. That being said there is often the situation where a custom typeface is required to give your application that particular look or branding. This post aims to show how to setup up a custom typeface for your application and what to think about while doing so.
1.Create a new project Start->newproject

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#FFFFFF" > <TextView android:id="@+id/txt1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="25dip" android:gravity="center" android:textColor="#ef0000" android:layout_marginTop="50dip" android:text="Welcome to Android Project" /> </LinearLayout>


2.Now open your MainActivity class file and try following code. In the following code i am importing font from assets folder and using TypeFace class i am applying custom font on textview label.


3.The ideal place to put your typefaces would be in the assets folder as hinted by the load method Typeface.createFromAsset(,). To my satisfaction the Android API  can load both .otf and .ttf files. I place my typface files as shown in the screenshot below. Although there is ni requirement to place it in a subfolder 



MainActivity.java:



package com.example.fonts; import android.app.Activity; import android.graphics.Typeface; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends Activity { String font; TextView txt1; public static Typeface tf; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //path to font font="fonts/Jellyka_Castle_s_Queen.ttf"; //label for textview txt1=(TextView)findViewById(R.id.txt1); //loading for font tf=Typeface.createFromAsset(MainActivity.this.getAssets(), "Jellyka_Castle_s_Queen.ttf"); //set font to textview txt1.setTypeface(tf); } }
3.Run the project.Here the output like this.

                             
                                        



No comments :

Post a Comment