Friday, 7 November 2014

Android - How to change the fontFamily on the TextView

I will show you how to change fontfamily and include external fontfamily  for textview, edittext and all the string show in the screen.

Please try it your self.

package com.j4android;


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

public class MainActivity extends Activity{
TextView text1, text2, text3; @Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.main);

                text1 = (TextView) findViewById(R.id.textView1);
                text2 = (TextView) findViewById(R.id.textView2);
        text3 = (TextView) findViewById(R.id.textView3);
     

        Typeface tf1 = Typeface.createFromAsset(getAssets(),
"fonts/albr65w.ttf");
        Typeface tf2 = Typeface.createFromAsset(getAssets(),
"fonts/Roboto-Medium.ttf");
        Typeface tf3 = Typeface.createFromAsset(getAssets(),
"fonts/Roboto-Italic.ttf");


                 text1.setTypeface(tf1);
         text2.setTypeface(tf2);

         text3.setTypeface(tf3);
           
       }


}


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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text2" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text3" />   


</LinearLayout>

Note:
Sample font :
    - albr65w.ttf
    - Roboto-Italic.ttf 
    - Roboto-Medium.ttf
Create folder "fonts" inside assets and put the above font into fonts. 

No comments:

Post a Comment