Open kbhuwalka opened 7 years ago
I am using a custom Font ttf file. Is it possible to use it directly from the xml?
I would suggest not using a custom font at all, however, if you must do so. Try this:
assests/fonts
Typeface typeface = Typeface.createFromAsset(getAssets(), "fontfilename.tff");
textView.setTypeface(typeface);
Note: getAssets()
requires a Context. Also ensure that the extension of the font is in lowercase.
Let me know how that works out for you.
If I do so, I'll have to give IDs to all the texviews where I want to use that font and I'll have to call all TextViews in different java classes and setType face for it. If there is any way to do something in xml, it would be better. Also, if I need to change the font I'll have to change it in just one CustomTextview class. But if I do the other way, I'll have to change the file name in each and every class.
If I do so, I'll have to give IDs to all the texviews where I want to use that font and I'll have to call all TextViews in different java classes and setType face for it. If there is any way to do something in xml, it would be better. Also, if I need to change the font I'll have to change it in just one CustomTextview class. But if I do the other way, I'll have to change the file name in each and every class.
This can be done easily come Android O, but let me look around a bit more. I will get back to you on this one.
It is advisable to not use custom views until you absolutely need to. This way you can prevent bloat from your code, making it more maintainable.
For your purpose of setting a typeface, try using the
android:typeface
attribute in the XML file.