androidandroid-canvasandroid-typeface

Canvas drawText (font) import Times font?


I'm trying to use Times as font for the drawText()-method of the Canvas element. However couldn't find a solution yet.

There is the possibility to set fonts like 'sans-serif' or 'casual' by using following code:

paint.setTypeface(Typeface.create("casual",Typeface.NORMAL));

However trying to use Times or Arial etc. doesn't work. Do I have to import these fonts first by myself?

Would appreciate if You have a solution for this. Thank You in advance!


Solution

  • This helped me to solve it: 1 - Creating a font directory in resources (res) folder. 2 - Drag & Drop .tff file in the font directory (example.tff). And do following:

    Typeface myFont = ResourcesCompat.getFont(this, R.font.times);
    

    Source (see accepted answer): Link