Wednesday, 9 April 2014

how to set font to textview in android


main.xml

<RelativeLayout 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" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="34dp"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="25dp"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="34dp"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="38dp"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView4"
        android:layout_centerVertical="true"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView5"
        android:layout_below="@+id/textView5"
        android:layout_marginTop="32dp"
        android:text="TextView" />
    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView6"
        android:layout_below="@+id/textView6"
        android:layout_marginTop="42dp"
        android:text="TextView" />
</RelativeLayout>

 MainActivity 

package com.ttfdemo;

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

public class MainActivity extends Activity {

   
    TextView textView1,textView2,textView3,textView4,textView5,textView6,textView7;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        textView1=(TextView)findViewById(R.id.textView1);
        textView2=(TextView)findViewById(R.id.textView2);
        textView3=(TextView)findViewById(R.id.textView3);
        textView4=(TextView)findViewById(R.id.textView4);
        textView5=(TextView)findViewById(R.id.textView5);
        textView6=(TextView)findViewById(R.id.textView6);
        textView7=(TextView)findViewById(R.id.textView7);       
       
         Typeface Text_Font_1 = Typeface.createFromAsset(this.getAssets(), "Courier.ttf");
         textView1.setTypeface(Text_Font_1);
       
         Typeface Text_Font_2 = Typeface.createFromAsset(this.getAssets(), "arial.ttf");
         textView2.setTypeface(Text_Font_2);
       
         Typeface Text_Font_3 = Typeface.createFromAsset(this.getAssets(), "gillsans.ttf");
         textView3.setTypeface(Text_Font_3);
       
         Typeface Text_Font_4 = Typeface.createFromAsset(this.getAssets(), "HelveticaNeue-Regular.ttf");
         textView4.setTypeface(Text_Font_4);
       
         Typeface Text_Font_5 = Typeface.createFromAsset(this.getAssets(), "HelveticaNeue-Roman.ttf");
         textView5.setTypeface(Text_Font_5);
       
         Typeface Text_Font_6 = Typeface.createFromAsset(this.getAssets(), "segoeui.ttf");
         textView6.setTypeface(Text_Font_6);
       
         Typeface Text_Font_7 = Typeface.createFromAsset(this.getAssets(), "trebuchet.ttf");
         textView7.setTypeface(Text_Font_7);
                
         textView1.setText("Courier.ttf");
         textView2.setText("arial.ttf");
         textView3.setText("gillsans.ttf");
         textView4.setText("HelveticaNeue-Regular.ttf");
         textView5.setText("HelveticaNeue-Roman.ttf");
         textView6.setText("segoeui.ttf");
         textView7.setText("trebuchet.ttf");
      }
  }

No comments:

Post a Comment