Monday, 14 January 2013

Android use of external Fonts through custom views in Android

In this blog , you will find a very optimal way how to use external fonts  by using Custom view for TextView, EditText and Button.

Let us assume we have to use Custom fonts through out the application. Then its very difficult to set font to each and every view.

 Font font1 = Typeface.createFromAsset(getAssets(), "fonts/Arial.ttf");

Now we need to set this font to every widget like TextView,Button, EditText..
So explicitly we need to setFont(font1) to each view.

e.g
         TextView Title = (TextView) findViewById(R.id.title);
         TextView Summary=(TextView)findViewById(R.id.summary);
        TextView Desc = (TextView) findViewById(R.id.description);       

        Title.seFont(font1);
        Summary.setFont(font1);
        Desc.setFont(font1);

Solution:--
So to over come this issue we will create Customview for TextView. Like wise we can achieve for Button and EditText also.

Step1) Create a package for custom component.
let us suppose com.amit.customview

a) create a class name as CustomTextView and copy paste this code but make sure that the custom font should be Project Assets folder. So you can replace font name.
package com.amit.customview;

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

public class CustomTextView extends TextView{

    public CustomButton(Context context) {
        super( context );
        setFont();

    }

    public CustomTextView (Context context, AttributeSet attrs) {
        super( context, attrs );
        setFont();
    }

    public CustomTextView (Context context, AttributeSet attrs, int defStyle) {
        super( context, attrs, defStyle );
        setFont();
    }

    private void setFont() {
        Typeface normal = Typeface.createFromAsset(getContext().getAssets(),"fonts/Arial.ttf");
        setTypeface( normal, Typeface.NORMAL );

    }
}

b) Create class named as CustomEditText inside the same package and follow the same
package com.amit.customview;

import android.content.Context;
public class CustomEditText extends EditText{

    public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomEditText(Context context) {
        super(context);
        init();
    }

    private void init() {
          Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Arial.ttf");
            setTypeface(tf);
         }

}

c) Like wise create class named as CustomButton inside the same package

package com.amit.customview;

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

public class CustomButton extends Button{

    public CustomButton(Context context) {
        super( context );
        setFont();

    }

    public CustomButton(Context context, AttributeSet attrs) {
        super( context, attrs );
        setFont();
    }

    public CustomButton(Context context, AttributeSet attrs, int defStyle) {
        super( context, attrs, defStyle );
        setFont();
    }

    private void setFont() {
        Typeface normal = Typeface.createFromAsset(getContext().getAssets(),"fonts/Arial.ttf");
        setTypeface( normal, Typeface.NORMAL );

    }
}

Step 2) Now next how to use  this CustomView inside our application
We can use it both the way either through xml layout or in Activty class.

a) First we will see how to use it from xml layout.
Now instead  of Textview we will use like this,
 <LinearLayout
        android:id="@+id/signing_details"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical"
        android:weightSum="100" >

<com.amit.customview.CustomTextView
                android:id="@+id/textview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                 android:text="@string/app_name"
                 android:textSize="16sp" />
 <com.amit.customview.CustomEditText
                android:id="@+id/username"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                 android:hint="@string/userName"
                 android:textSize="16sp" />
<com.amit.customview.CustomButton
                android:id="@+id/signin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                 android:text="@string/button"
                 android:textSize="16sp" />

</ LinearLayout>


b) Other we can use it dynamically in Activity.

        CustomTextView textView = new CustomTextView (this);
        textView.setText(res.getString(R.string.app_name));
        textView.setTextColor(Color.parseColor("#000000"));
        textView.setTextSize(16);

Like wise we can use CustomEditText and CustomButton.

Hope this help you lot..
Please fell free to give your comment to my personnel email id
amithigh.gupta@gmail.com

3 comments:

  1. Wow, What a Excellent post. I really found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.Thanks download for pc

    ReplyDelete
  2. Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work. etcher.download

    ReplyDelete
  3. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. https://techbomb.net/

    ReplyDelete