Apply External Font in Whole Android Application

by Wednesday, November 12, 2014 0 comments

Implementing custom font in whole application is very easy. We can set any custom font to our whole android application just like a theme. This is very simple procedure and easy to implements. 

Needs:

  • Custom font (stored in Asset folder)
  • Commonfont util to apply font for TextView,Button,EditText,CheckBox etc.. in whole application


CommonFont.java


 import android.content.Context;  
 import android.graphics.Typeface;  
 import android.util.Log;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import android.widget.Button;  
 import android.widget.CheckBox;  
 import android.widget.EditText;  
 import android.widget.RadioButton;  
 import android.widget.TextView;  
 public class CommonFont {  
      public static void setfont(final Context context, final View viewroot, final String fontpath) {  
           try {  
                if(viewroot instanceof ViewGroup) {  
                     ViewGroup viewgroup = (ViewGroup) viewroot;  
                     int childCount = viewgroup.getChildCount();  
                     for (int i = 0; i < childCount; i++){  
                          setfont(context, viewgroup.getChildAt(i),fontpath);  
                     }  
                }else if (viewroot instanceof TextView)  
                     ((TextView) viewroot).setTypeface(Typeface.createFromAsset(context.getAssets(), fontPath));  
                 else if (viewroot instanceof Button)  
                          ((Button) viewroot).setTypeface(Typeface.createFromAsset(context.getAssets(), fontPath));  
                 else if (viewroot instanceof EditText)  
                          ((EditText) viewroot).setTypeface(Typeface.createFromAsset(context.getAssets(), fontPath));  
                 else if (viewroot instanceof CheckBox)  
                          ((CheckBox) viewroot).setTypeface(Typeface.createFromAsset(context.getAssets(), fontPath));  
           } catch (Exception e) {  
                e.printStackTrace();  
           }  
      }  
 }

In Activity:
Just use like below

 CommonFont.setfont(context, findViewById(R.id.container),"font/Roboto.ttf");  

 View view;  
 CommonFont.setfont(context, view,"font/Roboto.ttf");  

karthik nagarajan

Androider

Welcome to Android-Action Blog. I’m a normal guy, who is passionate about Mobile Coding. Here I am writing about Android. Happy learning

0 comments:

Post a Comment