Reputation: 29
I'm trying to change font of a TextView to a custom one. Have read this other question and tried instructions as outlined here. The font in question is Roboto Thin, downloaded straight from Android Design and is in ttf.
The code at the main activity class is as follows:
TextView txt = (TextView) findViewById(R.id.hometext);
Typeface font = Typeface.createFromAsset(getAssets(), "Roboto-Thin.ttf");
txt.setTypeface(font);
// "hometext" is the TextView I wish to change font of.
App does not crash when the above 3 lines are commented, which I hope isolates the problem.
Other etc info:
Also tried "fonts/Roboto-Thin.ttf" and "assets/fonts/Roboto-Thin.ttf", both don't work.
01-17 22:38:29.086: D/AndroidRuntime(17774): Shutting down VM
01-17 22:38:29.086: W/dalvikvm(17774): threadid=1: thread exiting with uncaught exception (group=0x415f4ba8)
01-17 22:38:29.106: E/AndroidRuntime(17774): FATAL EXCEPTION: main
01-17 22:38:29.106: E/AndroidRuntime(17774): Process: sp.com, PID: 17774
01-17 22:38:29.106: E/AndroidRuntime(17774): java.lang.RuntimeException: Unable to start activity ComponentInfo{sp.com/sp.com.POSServices}: java.lang.RuntimeException: native typeface cannot be made
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.os.Handler.dispatchMessage(Handler.java:102)
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.os.Looper.loop(Looper.java:136)
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-17 22:38:29.106: E/AndroidRuntime(17774): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 22:38:29.106: E/AndroidRuntime(17774): at java.lang.reflect.Method.invoke(Method.java:515)
01-17 22:38:29.106: E/AndroidRuntime(17774): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-17 22:38:29.106: E/AndroidRuntime(17774): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-17 22:38:29.106: E/AndroidRuntime(17774): at dalvik.system.NativeStart.main(Native Method)
01-17 22:38:29.106: E/AndroidRuntime(17774): Caused by: java.lang.RuntimeException: native typeface cannot be made
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.graphics.Typeface.<init>(Typeface.java:175)
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.graphics.Typeface.createFromAsset(Typeface.java:149)
01-17 22:38:29.106: E/AndroidRuntime(17774): at sp.com.POSServices.onCreate(POSServices.java:18)
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.app.Activity.performCreate(Activity.java:5231)
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-17 22:38:29.106: E/AndroidRuntime(17774): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-17 22:38:29.106: E/AndroidRuntime(17774): ... 11 more
Upvotes: 0
Views: 2424
Reputation: 1185
You should search for related questions first, because it says that TypeFace cannot be made. Did you give the correct path? If so, test other fonts too.
Same problem from before: "Native typeface cannot be made" only for some people
Also, use the Fontify Library for Android. It makes everything much easier and better!
URL: https://github.com/danh32/Fontify
Upvotes: 0
Reputation: 2692
Ok there is a bug in android! This should work!
public class Typefaces {
private static final String TAG = "Typefaces";
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
public static Typeface get(Context c, String assetPath) {
synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
Typeface t = Typeface.createFromAsset(c.getAssets(),
assetPath);
cache.put(assetPath, t);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface '" + assetPath
+ "' because " + e.getMessage());
return null;
}
}
return cache.get(assetPath);
}
}
}
Now just call Typefaces.get(this,"address of font here")
I referred from here.
Good luck!
Upvotes: 1
Reputation: 2621
Have a look at where your font is. If its in assets/fonts then the code for typeface should be:
Typeface.createFromAsset(getAssets(), "fonts/Roboto-Thin.ttf");
Upvotes: 0