Thursday, 19 November 2015

How to create custom font for Text View

Hi guys i am going to explain custom font for TextView

First We have create assets directory inside the main directory and then create fonts directory inside the assets main->assets->font->  then put the font file inside the font directory

Then create the below file inside any one package. The below file created in com.crm.common package

package com.crm.Common;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

/** * Created by Ashok on 11/19/2015. */public class CustomReqularTextView extends TextView {
    public static Typeface FONT_NAME;


    public CustomReqularTextView(Context context) {
        super(context);
        if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/OpenSans-Regular_0.ttf");
        this.setTypeface(FONT_NAME);
    }
    public CustomReqularTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/OpenSans-Regular_0.ttf");
        this.setTypeface(FONT_NAME);
    }
    public CustomReqularTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if(FONT_NAME == null) FONT_NAME = Typeface.createFromAsset(context.getAssets(), "fonts/OpenSans-Regular_0.ttf");
        this.setTypeface(FONT_NAME);
    }
}


Then We have to use like

<com.crm.Common.CustomBoldTextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:id="@+id/compid"    android:text="@string/compname"    android:layout_gravity="center_horizontal"/>

No comments:

Post a Comment