0

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!

Sercan Samet Savran
  • 755
  • 1
  • 9
  • 20

2 Answers2

1

First you need to create an 'assets' folder under the 'main' folder and put a font file like .ttf file in it.

assets folder path

this is casual font download link

https://befonts.com/casual-font.html

I hope this is the answer you want. thank you

example)

Typeface mTfRegular = Typeface.createFromAsset(getContext().getAssets(), "OpenSans-Regular.ttf");
setTypeface(mTfRegular)

Another easy way to do this is to use the font family.

this way,

Create a folder called font in the /app/src/main/res/ path, put the font file inside, and write the contents of the Font Family in xml in /app/src/main/res/xml, and then apply the TextView or EditText you want to apply. Apply it using the android: fontFamily property.

Writing the Font Family in XML,

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/casual_regular" />
    <font
        android:fontStyle="italic"
        android:fontWeight="400"
        android:font="@font/casual_italic" />
</font-family>

Apply Fonts to FontView Using TextView,

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/casual"/>
Genesis
  • 358
  • 2
  • 8
  • Yeah what should I do after that? Or is it done by putting the font in the asset folder? Will it work to call Typeface.create(„filename“...) ? I don‘t get the point. Hmm. – Sercan Samet Savran Feb 21 '20 at 01:41
  • You must do this by putting fonts in the Assets folder. – Genesis Feb 21 '20 at 02:46
  • If you are going to use the second method (Font Family), you should put it in the font folder, not in the assets folder. – Genesis Feb 21 '20 at 04:02
  • I do have getBaseContext().getAssets() or getApplicationContext().getAssets(). However with both of these, it isn't possible for the App, to find the font... – Sercan Samet Savran Feb 21 '20 at 11:41
0

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

Sercan Samet Savran
  • 755
  • 1
  • 9
  • 20