Android - SPLessons

Android Custom Fonts

Chapter 18

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Android Custom Fonts

Android Custom Fonts

Fonts have always been given a significant importance in UI design. That is why Android allows you to define your own fonts in the application. Android Custom Fonts lets you to define custom fonts for the strings in your application. In order to add your own fonts, you need to follow the steps given below:

• Download the required font from the internet, and then place it in the assets folder(.ttf/.otf ect). Its syntax is given below:

• Call the static method of Typeface class createFromAsset() to get your custom font from the assets. Its syntax is given below:

Ex:

Typeface Hneue = Typeface.createFromAsset(getAssets(), "HelveticaNeue.otf");

• Set this custom font object to your TextView Typeface property. You need to call setTypeface() method to do that. Its syntax is given below:

Ex:

tv1.setTypeface(Hneue);

The following example shows how to do this.

Create MainActivity.java under src/<your packagename>

MainActivity.java:

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //set activity content to external view
        setContentView(R.layout.activity_main);

        //find views by Id
        TextView tv1=(TextView)findViewById(R.id.textView1);
        TextView tv2=(TextView)findViewById(R.id.textView2);
        TextView tv3=(TextView)findViewById(R.id.textView3);
        TextView tv4=(TextView)findViewById(R.id.textView4);
        TextView tv5=(TextView)findViewById(R.id.textView5);

        //create customfonts
        Typeface Hneue = Typeface.createFromAsset(getAssets(), "HelveticaNeue.otf");
        Typeface contrast = Typeface.createFromAsset(getAssets(), "contrast.ttf");
        Typeface molten = Typeface.createFromAsset(getAssets(), "molten.ttf");
        Typeface oneway = Typeface.createFromAsset(getAssets(), "oneway.ttf");
        Typeface mypager = Typeface.createFromAsset(getAssets(), "mypager.ttf");

        //settypeface
        tv1.setTypeface(Hneue);
        tv2.setTypeface(contrast);
        tv3.setTypeface(molten);
        tv4.setTypeface(oneway);
        tv5.setTypeface(mypager);
    }
}

Create activity_main.xml in res/layout folder

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:gravity="center"
    android:background="#FFFFFF">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:textSize="25sp"
        android:textColor="#009ACD"
        android:text="Custom Fonts" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:textColor="#009ACD"
        android:layout_marginTop="30dp"
        android:text="Custom Fonts" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:textSize="25sp"
        android:textColor="#009ACD"
        android:text="Custom Fonts" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:textColor="#009ACD"
        android:layout_marginTop="30dp"
        android:text="Custom Fonts" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:textSize="25sp"
        android:textColor="#009ACD"

        android:text="Custom Fonts" />

</LinearLayout>

AndroidManifest.xml:

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.spl.myproject.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>

Output: