Nov 28, 2012

Display custom TrueType Font (TTF)

To display text using custom TTF; create /assets/fonts folder and save your ttf files in it. Create custom Typeface using Typeface.createFromAsset() method and apply it on views by calling setTypeface() method.

Display custom TrueType Font (TTF)


package com.example.androidttf;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  TextView testText = (TextView)findViewById(R.id.testtext);
  Button testButton = (Button)findViewById(R.id.testbutton);
  EditText testEditText = (EditText)findViewById(R.id.testedittext);
  
  Typeface typeface_Abbeyline = Typeface.createFromAsset(getAssets(), "fonts/Abbeyline.ttf");
  Typeface typeface_36daysag = Typeface.createFromAsset(getAssets(), "fonts/36daysag.ttf");
  Typeface typeface_AtomicClockRadio = Typeface.createFromAsset(getAssets(), "fonts/AtomicClockRadio.ttf");
  testText.setTypeface(typeface_Abbeyline);
  testButton.setTypeface(typeface_36daysag);
  testEditText.setTypeface(typeface_AtomicClockRadio);
 }

}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <TextView
        android:id="@+id/testtext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="I'm a TextView" />
    <Button
        android:id="@+id/testbutton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="I'm a Button" />
    <EditText
        android:id="@+id/testedittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


No comments:

Post a Comment

Infolinks In Text Ads