android set custom font to a paint android set custom font to a paint android android

android set custom font to a paint


If by "custom font" you mean a font that you are supplying as an asset, the following code should work:

Typeface plain = Typeface.createFromAsset(assetManager, pathToFont); Typeface bold = Typeface.create(plain, Typeface.DEFAULT_BOLD)Paint paint = new Paint();paint.setTypeface(bold);canvas.drawText("Sample text in bold",0,0,paint);


If you are using Android's new Fonts in XML for your fonts, then to get the typeface used for paint you can use:

val customTypeface = ResourcesCompat.getFont(context, R.font.myfont)

or if your min Android API >= 26

val customTypeface = resources.getFont(R.font.myfont)

Then to apply it to your paint object:

mTextPaint.typeface = customTypeface

For more info check out https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml#fonts-in-code


Use this for paint class:

 Paint paint = new Paint();   paint.setTypeface(Typeface.create("Arial",Typeface.ITALIC));