how to use the default typeface when failed to load custom typeface?
Question
I'm trying to load custom typeface.
It's working okay in a new model phone, but making an error in old model when loading custom typeface.
I think old model phone doesn't support the custom typeface. I guess i need to make it alternative with default typeface.
So I tried "try and catch" for alternative way.
But it still make the same error.
Shall anybody see my code and provide any suggestions? thank you.
face = Typeface.createFromAsset(getAssets(), "fonts/letter.ttf");
face2 = Typeface.SANS_SERIF;
try{
noteET.setTypeface(face);
}catch(Exception e){
noteET.setTypeface(face2);
}
In Note.java
, it has custom EditText
which contains backgound notepad lines.
I think because the custom font doesn't support it, the line height for drawing line can't be calculated.
Error Message is Below:
**10-31 07:09:05.275: ERROR/AndroidRuntime(5159): FATAL EXCEPTION: main
**10-31 07:09:05.275: ERROR/AndroidRuntime(5159): java.lang.ArithmeticException: divide by zero**
**10-31 07:09:05.275: ERROR/AndroidRuntime(5159): at myapp.secretdiary.second.Note$EditOnList.onDraw(Note.java:195)**
10-31 07:09:05.275: ERROR/AndroidRuntime(5159): at android.view.View.draw(View.java:6740)
10-31 07:09:05.275: ERROR/AndroidRuntime(5159): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
10-31 07:09:05.275: ERROR/AndroidRuntime(5159): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
10-31 07:09:05.275: ERROR/AndroidRuntime(5159): at android.view.View.draw(View.java:6743)
10-31 07:09:05.275: ERROR/AndroidRuntime(5159): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
10-31 07:09:05.275: ERROR/AndroidRuntime(5159): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
10-31 07:09:05.275: ERROR/AndroidRuntime(5159): at android.view.View.draw(View.java:6743)
10-31 07:09:05.275: ERROR/AndroidRuntime(5159): at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
10-31 07:09:05.275: ERROR/AndroidRuntime(5159): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
10-31 07:09:05.275: ERROR/AndroidRuntime(5159): at android.view.View.draw(View.java:6743)
10-31 07:09:05.275: ERROR/AndroidRuntime(5159): at android.widget.FrameLayout.draw(FrameLayout.java:352)
*********more************
Note.Java:195
line source is below
public static class EditOnList extends EditText{ //Noepad Line Drawing
public EditOnList(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Paint p = new Paint();
p.setColor(Color.BLUE);
int count = getLineCount();
int height = this.getHeight();
int line_height = this.getLineHeight();
int size = height/line_height+1; // line 195
if (count<size)count=size;
int y = 2;
for(int i=1; i<count; i++){
y += line_height;
canvas.drawLine(0+60, y, this.getRight()-60, y, p);
}
}
} // EditOnList
Solution
Your catch isn't necessary. When the custom ttf can't be loaded and you caught this error so that your App doesn't crash the default Typeface is choosen automatically.