MaraviAnurag / IdiotBox

Movie App
0 stars 0 forks source link

Don't use custom Textview #1

Open kbhuwalka opened 7 years ago

kbhuwalka commented 7 years ago

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.

MaraviAnurag commented 7 years ago

I am using a custom Font ttf file. Is it possible to use it directly from the xml?

kbhuwalka commented 7 years ago

I would suggest not using a custom font at all, however, if you must do so. Try this:

  1. Import the font in the following sub-directory: assests/fonts
  2. Assign it to your textView using the following Java code:
    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.

kbhuwalka commented 7 years ago

Let me know how that works out for you.

MaraviAnurag commented 7 years ago

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.

MaraviAnurag commented 7 years ago

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.

kbhuwalka commented 7 years ago

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.