Custom Font in Android

Frequently used code samples and tutorials
vivekkalady
Site Admin
Posts: 35
Joined: Tue Nov 18, 2014 2:39 pm

Custom Font in Android

Postby vivekkalady » Fri Nov 21, 2014 2:32 pm

Method 1

This is the basic code for setting Font to a textview


  • create a folder called fonts in Assets folder of the project
  • then copy the font file (.ttf) to the fonts directory
  • the write the below code

Code: Select all

public class MainActivity extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      TextView tx = (TextView)findViewById(R.id.hello);
      Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/Roboto.ttf");
      tx.setTypeface(custom_font);
   }

}



Method 2

If you want to use this for all textviews, then you can create a custom class which extends TextView

Code: Select all

public class MyTextView extends android.widget.TextView
{

    public MyTextView(Context context)
    {
        this(context, null);
    }

    public MyTextView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public MyTextView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs);
        if (this.isInEditMode()) return ;

        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SomeStyle);
        final String customFont = a.getString(R.styleable.SomeStyle_font);

        //Build a custom typeface-cache here!
        this.setTypeface(
            Typeface.createFromAsset(context.getAssets(), customFont)
        ); 
    }
}



Add this to attrs.xml

Code: Select all

<declare-styleable name="SomeStyle">
    <attr name="font" format="string" />
</declare-styleable>



Then in your theme, do this: This will make sure all the textviews will use the style MyTextView

Code: Select all

<item name="android:textViewStyle">@style/MyTextView</item>



And now we can define your custom font using your custom attribute in this style.

Code: Select all

<style name="MyTextView" parent="@android:style/Widget.TextView">
    <item name="font">MyPathToFontInAssets.ttf</item>
</style>


So whenever you use MyTextView in the project, it will have your custom font.


Method 3

Apply Font to all text in Application, you can traverse through full views and if it is an instance of textview then change the typeface


Code: Select all


// initialize the fonts to be used first
private static void initializeFonts(final Context context) {
    FONT_REGULAR = Typeface.createFromAsset(context.getAssets(), "fonts/myfont_medium.otf");
    FONT_BOLD = Typeface.createFromAsset(context.getAssets(), "fonts/myfont_bold.otf");
}

//functions to override the font by checking instanceof

public static void overrideFonts(final Context context, final View v) {
    try {
        if (v instanceof ViewGroup) {
            ViewGroup vg = (ViewGroup) v;
            for (int i = 0; i < vg.getChildCount(); i++) {
                View child = vg.getChildAt(i);
                overrideFonts(context, child);
            }
        } else if (v instanceof TextView) {
            ((TextView)v).setTypeface(FONT_REGULAR);
        }
    } catch (Exception e) {
        e.printStackTrace();
        // ignore
    }
}



If you use a subclass of Activity like MyAppActivity (extends Activity), then you don't need to change each and every Activity class for such customizations. Instead you may cut into it and override the behavior as such;

Code: Select all

public class MyAppActivity extends Activity {
... ...
    @Override
    public void setContentView(final int layoutResID) {
        super.setContentView(layoutResID);
        Utils.overrideFonts(this, findViewById(android.R.id.content));
    }
... ...
}


This way you can use any activity of yours to have common behavior;


Code: Select all

public class SettingsUI extends MyAppActivity {
... ...
}

Return to “Tutorials & Examples”

Who is online

Users browsing this forum: No registered users and 1 guest

cron