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
i'm writing this post because I have a problem with android studio on ubuntu mate 17.04.
The problem is that android studio doesn't found a custom font.
I have add this font on:
-main
---assets
------fonts
------------myfont.ttf
but when i do this:
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/FUTRFW.TTF");
classibutton.setTypeface(typeface);
i have an exception that show myfont is not found.
I attach the structure of my project:
how can i solve this??
Thanks all for the help
try this,I think .ttf should not be capital
classibutton = (Button)findViewById(R.id.button1);
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/FUTRFW.ttf");
classibutton.setTypeface(typeface);
try this your have to bind your classibutton than your can set Custom fonts to your and also
don't write .ttf in capital letters
sample code
classibutton=(Button)findViewById(R.id.button1);
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/FUTRFW.ttf");
classibutton.setTypeface(typeface);
Font file name should be in small letters. So First of all you should rename your font file as "futrfw.ttf" and then
classibutton=(Button)findViewById(R.id.button1);
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/futrfw.ttf");
classibutton.setTypeface(typeface);
And please be sure that your folder name should be "fonts" (as you are using "fonts/futrfw.ttf" here) and this folder must be inside the "assets" folder which is inside "main" folder.
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.
Im having some trouble with setting textview to invisible/visible.
basicly i want this to happen when an on/off button has been clicked.
what i did is kind of like
textview.setVisibility(TextView.VISIBLE);
textview.setVisibility(TextView.INVISIBLE);
when i try executing this the emultor says that the app has stopped unexcpetedly
Are you building this from XML or programmatically?
I would make it with an XML file then when the Activity runs change the property. Be sure to use setContentView(R.layout.main); before you try to get the TextView with findViewById(...).
Call .setVisibility(View.GONE); on the TextView to hide it.
Call .setVisibility(View.VISIBLE); to on the TextView to show it.
I have an example that does something like this. You can see the code here: https://github.com/ethankhall/Morse-Messenger/blob/master/src/com/kopysoft/MorseMessenger/Translate.java
Without more code or a stack trace, it's hard to say, but it sounds like you haven't initialized the text view. Here's how to do it:
TextView myTextView = (TextView) findViewById(R.id.tv_text);
Where 'tv_text' is the id of the textview as defined in the xml layout file.
Hope that helped!
Read about DDMS and logcat to obtain a stacktrace and to see what the problem is: http://developer.android.com/guide/developing/debugging/debugging-projects.html
This is what you are looking for:
I cannot get this to work so I thought it might be a wise idea posting over here...
I have a context menu in SWT (actually its an Eclipse plugin). It's a cascaded menu, so it expands as soon as you hover over a certain entry...
My problem is, that I want to attach a small icon to the menu but I struggle with that!
Code:
....
manager.add(new Separator());
// icon for the "change color" menu
ImageDescriptor icon = ImageDescriptor.createFromFile(null,
"icons/palette_brush.png");
// submenu
MenuManager colorMenu = new MenuManager("Menu", icon, null);
// Actions
colorMenu.add(someAction);
// add the action to the submenu
manager.add(colorMenu);
....
My problem is, that the new MenuManager can either be called with 2 Arguments (no attached image) or 3 (with attached image). The image should be passed along as ImageDescriptor.
The question basically is:
"How can I get an Imagedescriptor off an image?"
Maybe it's an stupid mistake - but I cannot get an ImageDescriptor from an image file. I have an *.png icon ready to use, but I struggle incorporating this.
If anyone could help out with a snippet, that would get me an ImageDescriptor from an image file this would be soo awesome!
Best regards!
MenuManager Documentation:
MenuManager Docu
Bundle bundle = Platform.getBundle(pluginId);
URL fullPathString = BundleUtility.find(bundle, "icons/palette_brush.png");
ImageDescriptor.createFromURL(fullPathString);
pluginId is the id of the plugin where you put your icon.
Bundle bundle= org.eclipse.core.runtime.Platform.getBundle(pluginId);
URL fullPathString = bundle.getEntry("icons/palette_brush.png");
desc= ImageDescriptor.createFromURL(fullPathString);
desc.createImage();