I want to use custom font in my android plugin library while using AlertDialog. Now here is the code to display AlertDialog using custom font.
AlertDialog alertDialog = new AlertDialog.Builder(mainActivity).setTitle("Title").setMessage("Message Text").create();
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", dialogOnClickListener);
TextView alertMessageTextView = (TextView) alertDialog.findViewById(R.id.message); // I am getting error here
TextView alertTitleTextView = (TextView) alertDialog.findViewById(android.R.id.title); // I am getting error here
Typeface customFont = Typeface.createFromAsset(mainActivity.getResources().getAssets(), "fonts/lobster-regular.ttf");
alertMessageTextView.setTypeface(customFont);
alertTitleTextView.setTypeface(customFont);
alertDialog.show();
I am getting error:
Error Unity AndroidJavaException: java.lang.NoSuchFieldError: No static field message of type I in class Lcom/cptech/pluginexample/R$id; or its superclasses (declaration of 'com.cptech.pluginexample.R$id' appears in base.apk)
I have placed my font file in Unity's "Assets/Resources" Folder. Hence in final apk the font resources appear in "assets/fonts" folder. But however, this doesn't matter as, I can use custom font in other TextView(s).
The only problem is that it cannot find AlertDialog's message and title, as I believe. Any help would be appreciated.
Few changes:
Use show() instead of create() when building the AlertDialog:
AlertDialog alertDialog = new AlertDialog.Builder(mainActivity).setTitle("Title").setMessage("Message Text").show();
Then, replace this line:
TextView messageTextView = (TextView) alertDialog.findViewById(R.id.message);
With android.R.id.message instead, like so:
TextView messageTextView = (TextView) alertDialog.findViewById(android.R.id.message);
And make sure you're not accidentally doing import com.android.R at the top of your file. You need this change because the id message is not an asset in your module, it's a default asset from an android library. So the R you're using right now is coming from your package - you need to specify R from android.R.
Last thing quick, watch out for the variable name of the textview when you set the text, should be changed to messageTextView
For some reason on lollipop, I'm getting this error and since lollipop is so new I can't find out why anywhere and I have no idea how to figure it out. Thanks in advance Logcat error:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.bent.MissionaryTracker/com.bent.MissionaryTracker.MainActivity}:
java.lang.RuntimeException: Font asset not found helvetica.ttf
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.RuntimeException: Font asset not found helvetica.ttf
at android.graphics.Typeface.createFromAsset(Typeface.java:190)
at com.bent.MissionaryTracker.MainActivity.onCreate(MainActivity.java:57)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
... 10 more
EDIT: HERE IS THE CODE THAT THROWS THE ERROR
title = (TextView) findViewById(R.id.title);
Typeface font = Typeface.createFromAsset(getAssets(), "helvetica.ttf");
title.setTypeface(font);
I have helvetica.ttf in my assets folder in my project folders.
EDIT:
This app works on all devices up until 5.0 so for some reason 5.0 is not recognizing the file in my assets folder.
I tried to post a screenshot of it in my assets folder but I don't have enough reputation to post images.
If you are working on Android Studio make sure your asset folder is under main and not res
This worked for me
Now it's better to use for custom fonts this approach
Store fonts in res/font
To get typeface use :
Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);
I had this problem too before I realized I didn't put "fonts/" before the .otf file
Typeface font = Typeface.createFromAsset(getAssets(),"fonts/TTMastersDEMOBlack.ttf");
This worked for me. In my case it didn't work because I created assets folder in res and not in main.
If changing the directory doesn't work it could be due to a problem already fixed by the Android team but didn't make it in time for 5.0. It's all explained here:
https://code.google.com/p/android-developer-preview/issues/detail?id=608&thanks=608&ts=1404735239
The workaround (at least it was for me) is to fix the font files with TTX as mentioned in the linked issue.
Getting TTX to work can be a bit of a hassle if you have no python expertise so I ended up downloading Adobe's AFDKO that includes TTX. You have to run 'ttx yourfontfile.ttf' that'll turn yourfile to .ttx and then run it again 'ttx yourfontfile.ttx' to get a new .ttf that works with 5.0.
Clean the project, it helped me on android 6.0
Just place a assets folder inside main directory.
After do this:
Typeface typeface=Typeface.createFromAsset(getAssets(),"OpenSans-Light.ttf");
textView.setTypeface(typeface);
I faced this issue before. I deleted x.ttf and then undid the deletion, tried to run with no lock. So my fix was just to delete it and downloaded the font again and pasted it to fonts/ folder and every thing worked fine.
Ok Guys,
I ran into the same problem. First of all I made my font loading logic crash proof:
/**
* Init method. This method must be called before any other call in this class.
*/
public static void init () {
fontPlain = createFont( "helvetica.ttf" , Typeface.NORMAL );
fontBold = createFont( "helveticab.ttf", Typeface.BOLD );
}
/**
* Load font from assets font folder.
*/
public static Typeface createFont (String font, int style) {
Typeface typeface;
try {
AssetManager assets = Kit.getAppContext().getAssets();
typeface = Typeface.createFromAsset( assets, "fonts" + File.separator + font );
}
catch (RuntimeException e) {
// createFromAsset() will throw a RuntimeException in case of error.
Log.e( Constants.TAG, "Unable to create font: " + font, e );
typeface = Typeface.defaultFromStyle( style );
}
return typeface;
}
And then I also downloaded another font from this project in git:
https://github.com/dimanchec3/Tutoriapps-Android/blob/master/assets/Helvetica.ttf
I realized that my old font was 56KB and this new font is 84KB. I guess I had an older version of the font.
Please make sure you have the proper license before using this font.
Hello I'm trying to use a Custom font to my Button I've added the font on res/assets.
On my .java I have this code
Button btjoc = (Button) findViewById(R.id.btJoc);
Typeface font = Typeface.createFromAsset(getAssets(), "Heart Breaking Bad.ttf");
btjoc.setTypeface(font);
Button bttaula = (Button) findViewById(R.id.btTaula);
bttaula.setTypeface(font);
And it doesn't work... I don't know if I have to put something else, wish you can help me...
Thanks.
Do you see any logcat error? Do you have related font in your assets directory? try to change your font name in asset folder and your code to Heart_Breaking_Bad.ttf
you must moved it to main/assets instead of res/assets.
and see my answer here , maybe help you.
I'm trying to use a font I found on the internet, but the problem is that I get an FC with "native typeface cannot be made".
Here is the code in the getView of my ListVIew:
holder.tv_SuraName =(TextView)convertView.findViewById(R.id.Start_Name);
holder.tv_SuraName.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "suralist_font.ttf"));
Can anyone tell me why can I use the custom rom? You can get it HERE .. the file is .ttf
My problem was incorrect placement of the assets folder.
When using Android Studio the assets folder should be inside of the source sets e.g.
src/main/assets/
Found in this answer
The font file is either corrupt or unsupported for some reason. You can drop it on the SD card and load it from file, to make sure it's not a problem with your assets.
There are basically 4 things that can cause this:
You use the wrong extension
You have to place the fonts in the assets folder and not inside assets/fonts/
You misspelled the fonts
The fonts need to be lowercase (in my case the solution was to rename MyFont.ttf to myfont.ttf, strange)
Double check the extension isn't in caps, ie. suralist_font.TTF
Fonts often come seem to come that way and it might be overlooked.
I had this same problem, I created mine in assets>fonts>whatever.ttf and was getting the same error. I added the fonts extension (fonts/whatever.ttf) and it fixed the problem in every case.
holder.tv_SuraName =(TextView)convertView.findViewById(R.id.Start_Name);
holder.tv_SuraName.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "fonts/suralist_font.ttf"));
Rather old thread, but I want to add one more reason this can happen: You have to set the content view before calling typeface. See example below
Does not work:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView title = (TextView)findViewById(R.id.tvTitle);
Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/whatever.ttf");
title.setTypeface(font);
setContentView(R.layout.main);
}
Does work!
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // <-- call before typeface
TextView title = (TextView)findViewById(R.id.tvTitle);
Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/whatever.ttf");
title.setTypeface(font);
}
I am using android studio, in my case problem was with the path of assets folder, in Eclipse path for assets folder is res/assets/ but in Android Studio its src/main/assets/
So, i just move my font file to this path and finally problem solved.
Either your font file is corrupt else you making some mistake in spelling while calling your file name. I had same issue, it was because file name specified in asset folder was
roboto_black.ttf
and while declaring in java file, i have been spelling it as:
roboto_blak.ttf
If your .ttf files are in asset folder,then use:
Typeface type_bold = Typeface.createFromAsset(getAssets(), "roboto_black.ttf");
Else if it is in fonts folder, then use:
type_bold = Typeface.createFromAsset(getAssets(), "fonts/roboto_black.ttf");
Hope it helps!
Hope this will help
Typeface.createFromAsset leaks asset stream: http://code.google.com/p/android/issues/detail?id=9904
As CommonsWare states in this answer
I would guess that there is a problem with the font itself. That error
will be triggered when native code in the OS attempts to load the
typeface.
Anyway try this one:
holder.tv_SuraName = (TextView)convertView.findViewById(R.id.Start_Name);
Typeface Font = Typeface.createFromAsset(this.getAssets(),"suralist_font.ttf");
holder.tv_SuraName.setTypeface(Font);
I had the same problem with COMIC.TTF from comic sans ms of Windows.
Since the original file I placed in assets/fonts folder was in uppercase as spelt above, I had to type it exactly in uppercase.
Typeface.createFromAsset(getAssets(), "fonts/COMIC.TTF");
it turns out the problem was caused by the lower case:
Typeface.createFromAsset(getAssets(), "fonts/comic.ttf");
Please note that this is due to either a corrupt file or you have placed the file in the wrong folder.
According to Android documentation here
main/assets/
This is empty. You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.
Therefore your folder structure should be yourmodule/main/assets (e.g. a***pp/main/assets*** )
What worked for me was first changing my font name to only lower case and underscore: from Amatic-Sc.ttf to amatic_sc.ttf.
Then in the set.TypeFace method change assets/fonts/amatic_sc.ttf to just fonts/amatic_sc.ttf.
I've been debugging forever on this error in Android Studio. Clean and rebuilding the project after adding the fonts to the assets directory fixed the issue for me.
Just did a build --> clean and it started rendering .
I had same issue which happened on Android 4.0.3-4.0.4 (100% reproducible on emulator IceCreamSandwich mips 4.0.3).
The problem was that TTF fonts, that I had, were not correct, despite almost all Android versions were able to load those TTF font. So I used https://cloudconvert.com/woff-to-ttf (since I had woff files as well) to make new TTF fonts, which solved my problem.
I have a custom typeface which I have loaded in Typeface object with Typeface.createFromAsset(...). I need to drawing 1 symbom from this typeface with char-code 37 on canvas. I use this code:
String s=String.valueOf(((char)37));
Paint paint=new Paint();
paint.setTypeface(mNoteTypeface);
paint.setTextSize(30);
canvas.drawText(s, 20, 20, paint);
But result doesn't be good. I have the program for viewing fonts and one showed me that:
http://imagepost.ru/?v=qzvcgsbbxkldahdxuptuuvuyyugqlx.jpg
Help me to create a code, I'm very upset.
code shown is correct. Make sure mNoteTypeface contains custom font. Look for:
font file is in assets\fonts folder of project.
font files are case sensitive, "fonts\deco.ttf" will not work for "Deco.ttf" file:
paint.setTypeface(Typeface.createFromAsset(getAssets(),
"fonts/Deco.ttf"));