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:
- Native typeface cannot be made
- RuntimeException: native typeface cannot be made
- native typeface cannot be made, exception
- custom font in android studio
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:
- My
assets
folder is in 'app/src/main/assets' - My
fonts
folder is in 'app/src/main/assets/fonts' - 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
Droid X Screen Shot
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:
- The font file isn't "broken".
- The font location isn't wrong.
- The typed-in file name isn't wrong (extension, capitalization, etc.)
What else can I try?