Jitendra Prajapati
Jitendra Prajapati

Reputation: 1163

Custom Font for Entire Application

I want to change the font for entire application component (TextView, Edittext, button etc). I have found that I can setup style for application but here I am not able to put font from Asset folder to my custom style xml. I have to put my Custom TTF Font from asset folder to typeface element in style xml. I am not able to change monospace font to my custom font. My style is

<resources>
<style name="heading_text">
 <item name="android:textColor">#ff000000</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">16sp</item>
    <item name="android:typeface">monospace</item>      
</style>

Upvotes: 2

Views: 217

Answers (2)

Santhosh
Santhosh

Reputation: 1867

Use typeface from Utility class this can help in maintaining single instance for entire application.

private static Typeface typeface;


public static Typeface getTypeFace(Context context) {
    if (typeface == null) {
        typeface = Typeface.createFromAsset(context.getAssets(),
                "Sawasdee-Bold.ttf");
    }
    return typeface;
}

Upvotes: 0

Jitesh Upadhyay
Jitesh Upadhyay

Reputation: 5260

Hi visit my blog at http://upadhyayjiteshandroid.blogspot.in/2013/01/android-custom-fonts-part-2.html where you can also download the code as well.

what you need to do is make a custom view with particular needed font and use it wherever you want.

suppose there is a textview for this purpose make CustomTextView .java code is as follows

package com.jitesh.customfonts;

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

public class CustomTextView extends TextView {

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

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

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

  public void init() {

   Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
    "fonts/HandmadeTypewriter.ttf");
  setTypeface(tf, 1);

  }
}

use it as follows in xml

<com.jitesh.customfonts.CustomTextView
        android:id="@+id/custom1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="70dp"
        android:text="@string/hello_Jitesh"
        tools:context=".MainActivity" >
    </com.jitesh.customfonts.CustomTextView>

also make sure that yours font is available at asset with fonts/HandmadeTypewriter.ttf

Upvotes: 2

Related Questions