0

i'm trying to set a custom Font to all of my TextViews/EditViews and so on. And i followed this answere: Is it possible to set a custom font for entire of application?

For this i have a .ttf file in assets/Fonts/myFont.ttf. To add this Font to all of my TextViews i'm overriding the TextView-Class:

public class MyCustomTextView extends TextView {

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

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

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

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

}

To add this to my TextView i simply do in the layout:

 <com.android.faccess.MyCustomTextView
    android:id="@+id/textview"
    style="@style/textview" />

Where the Style is:

<style name="textview">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">28dp</item>
    <item name="android:layout_gravity">center_horizontal|center_vertical</item>
    <item name="android:textSize">20sp</item>
    <item name="android:layout_marginLeft">5dp</item>
    <item name="android:layout_marginRight">5dp</item>

The Error

I think i do miss something really simple. This is the LogCat:

12-17 10:16:21.633: E/AndroidRuntime(2094): android.view.InflateException: Binary XML file line #8: Error inflating class com.android.faccess.MyCustomTextView 12-17 10:16:21.633: E/AndroidRuntime(2094): Caused by: java.lang.RuntimeException: native typeface cannot be made 12-17 10:16:21.633: E/AndroidRuntime(2094): at android.graphics.Typeface.<init>(Typeface.java:175) 12-17 10:16:21.633: E/AndroidRuntime(2094): at android.graphics.Typeface.createFromAsset(Typeface.java:149) 12-17 10:16:21.633: E/AndroidRuntime(2094): at com.android.faccess.MyCustomTextView.init(MyCustomTextView.java:28) 12-17 10:16:21.633: E/AndroidRuntime(2094): at com.android.faccess.MyCustomTextView.<init>(MyCustomTextView.java:18) 12-17 10:16:21.633: E/AndroidRuntime(2094): ... 51 more

I'm developing for Android 4.0 and above so no Need to worry about leaks with this solution.

Any help is appreciated.

Community
  • 1
  • 1
MMike
  • 598
  • 3
  • 23

1 Answers1

0

Are you created a folder called fonts in assets or directly placed in assets folder??if folder not created create a folder and place your font file there . If no fonts folder use the path like this.

Typeface font = Typeface.createFromAsset(getAssets(), "myfont.TTF");

else

Typeface font = Typeface.createFromAsset(getAssets(), "fonts/myfont.TTF");

For more info try this

Community
  • 1
  • 1
Remees M Syde
  • 2,564
  • 1
  • 19
  • 42