3
votes

I'm using a font I found on the internet. In particular, the font that is currently causing a problem is Amaranth (Google Fonts link).

I've looked through these questions:

All of those questions seem to be dealing with crashes that occur on the device at runtime. My problem is that I get this Exception when trying to use Android Studio's Preview tool to preview my layout.

The text I'm seeing in the "Rendering Problems" pop-up is this:

Exception raised during rendering: native typeface cannot be made
java.lang.RuntimeException: native typeface cannot be made   
  at android.graphics.Typeface.<init>(Typeface.java:147)   
  at android.graphics.Typeface.createFromAsset(Typeface.java:121)   
  at app.SegmentButton.onDraw(SegmentButton.java:79)

The code in SegmentButton is this:

@Override
public void onDraw(Canvas canvas) {

    // Other code...

    String fontPath = "fonts/Amaranth-Bold.otf";
    // The next line is the line that causes the "crash"
    Typeface tf     = Typeface.createFromAsset(mContext.getAssets(), fontPath);
    textPaint.setTypeface(tf);

    // Other code...

}

I have checked and ensured the following items:

  1. My assets folder is in 'app/src/main/assets'
  2. My fonts folder is in 'app/src/main/assets/fonts'
  3. The file name is exactly "Amaranth-Bold.otf"

This font works exactly as desired when running my app. I have tested this on an LG Optimus Pro G as well as a Droid X. You can see the results in these images:

LG Optimus G Pro Screen Shot

enter image description here

Droid X Screen Shot

enter image description here

As you can see, the fonts show up as desired in both places. As you can probably also see, the Droid X screen shot shows some layout issues that I need to resolve. I'm trying figure out how to do this at design time using the tools built in to Android Studio. This is merely the first issue I'm tackling. I would hate to have to run the app each time in order to make small tweaks to the layout. This is especially true since these are the only two hardware devices I currently have, and I want to test on a much wider variety of screen sizes (and the emulator is sloooooooooow).

Based on my reading of other questions (and primarily based on the fact that the devices load the font fine at runtime), my current conclusions are:

  1. The font file isn't "broken".
  2. The font location isn't wrong.
  3. The typed-in file name isn't wrong (extension, capitalization, etc.)

What else can I try?

1

1 Answers

2
votes

I had the same problem yesterday. Android Studio gives you this tip:

Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE

Rewrite your onDraw() method like this:

@Override
public void onDraw(Canvas canvas) {            

    // Other code...                   
    if(!isInEditMode())changeFont();
    // Other code...

}

private void changeFont(){
    String fontPath = "fonts/Amaranth-Bold.otf";
    Typeface tf     = Typeface.createFromAsset(mContext.getAssets(), fontPath);
    textPaint.setTypeface(tf);
}