2

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>

Jitendra Prajapati
  • 1,002
  • 1
  • 10
  • 33

2 Answers2

2

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

Jitesh Upadhyay
  • 5,244
  • 2
  • 25
  • 43
0

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;
}
Santhosh
  • 1,867
  • 2
  • 16
  • 23
  • But I need to put this typeface in application theme. According to your answer I have to set this typeface in every button, edittext and text view. We just want to put this typeface in app theme so it may reflect in all over application. – Jitendra Prajapati Feb 17 '14 at 15:17