0

currently practicing making an application using android studio. I have this customized font and saved into res/font directory for the later use. Inside the activity, i am trying to call getfont(R.font.my_customized_font) with the typeface object. ex) Typeface font = getResources().getfont(R.font.mycustomizedfont)

However, when i try to run the project with the actual device which has the API level 24, it won't activate since the getfont() method requires the min sdk level with 26.

I know that you can set the font of text view in the xml file by doing android:fontfamily but i am using the textswitcher instead of textview.

Emulator works fine by setting up min sdk with 26 but I would like to figure this out by using the actual device.

I found the solution by reading through the developer's website I just added Typeface font = ResourcesCompat.getfont(context, R.font.mycustomizedfont)

  • https://github.com/chrisjenx/Calligraphy may help you – Northern Poet Nov 16 '17 at 08:20
  • check this [https://stackoverflow.com/a/16883281/5308778](https://stackoverflow.com/a/16883281/5308778) – yashkal Nov 16 '17 at 08:23
  • @NorthernPoet thanks for the library I found that library called Calligraphy from the different stackoverflow answers as well. But I was hesitant in implementing it since i am just practicing developing app from the android developer's website. Just quick question, from the following library its telling me to save the font file to the assets directory but android developer website tells me there is a font directory under the res folder. Would the library still work if give the directpath of the font from the res file or would that cause a problem? – Hyun Suk Lee Nov 16 '17 at 08:51
  • @HyunSukLee, no. Calligraphy library works only using fonts from `asset` directory. But the huge benefit of this library is applying font to every text view in application by few lines of code. – Northern Poet Nov 17 '17 at 00:30
  • @NorthernPoet thanks in advance, i figured out and edited the question by using typeface and resourcescompat library! I would like to give you a check mark for your response but don't know how... but anyway thanks for explaining the advantage of using cali library. – Hyun Suk Lee Nov 17 '17 at 08:54

3 Answers3

0

I use a Min API of 21 and this works for me:

Typeface type = Typeface.createFromAsset(getAssets(),"fonts/mycustomizedfont");

Then, to set the font to a the child TextViews:

text_view.setTypeface(type);

Hope this helps!

  • I was trying that answer but just wandering like above comment. Android Studio seems like providing font directory under the res directory. Is it possible to call typeface.createfromresource rather than calling it from the asset? – Hyun Suk Lee Nov 16 '17 at 09:09
0

You can use via xml layout simple is that just use like this:-

android:fontFamily="@fonts/avenir"

Instead of avenir use your font name under res/font folder. Hope it will help you.

0

I found the solution by reading through the developer's website I just added Typeface font = ResourcesCompat.getfont(context, R.font.mycustomizedfont)

to apply the font programmatically