Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How android set custom font in canvas?

hi i want to change my font size by using paint , canvas in android. My code is here. how can i do this ?

public class MainActivity extends Activity 

{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
Canvas canvas = new Canvas();
Typeface tf = Typeface.createFromAsset(getAssets(), "RECOGNITION.ttf");
     Paint paint = new Paint();
     paint.setTypeface(tf);
     canvas.drawText("Lorem ipsum", 0, 0, paint);


}
}

can any body help me to solve problem ? i read some tutorials but not under stand . i have read some post of Stack ,facing some problems.

like image 822
NadeemYousaf Avatar asked Nov 29 '13 05:11

NadeemYousaf


People also ask

What format does Android use for fonts?

Fonts are compiled in R file and are automatically available in the system as a resource. You can then access these fonts with the help of the font resource type.

How to add custom fonts to text in Android Studio?

Appropriate fonts do not just enhance the user interface but they also signify and emphasize the purpose of the text. There are majorly three methods to add custom fonts to text in Android Studio. The first two methods involve the use of the Typeface class while the last method is quite direct and easy.

How to change font of textview in Android?

The android:fontFamily attribute of the TextView class is used to specify the font. Step 1: Go to the XML file and go to the Design view. Step 2: Click the TextView you want to change the font of. Step 3: In the search bar, search for fontFamily. Step 4: In the dropdown menu, you can check out the fonts available.

How to add a font to an existing paint file?

create "fonts" folder under "assets" folder. After that put your font file in "fonts" folder and write below code. Typeface tf =Typeface.createFromAsset (getAssets (),"fonts/YOURFONT.ttf"); Paint paint = new Paint (); paint.setTypeface (tf); canvas.drawText ("Sample text in bold RECOGNITION",0,0,paint);

How to get the typeface used for paint?

Did you try getContext ().getAssets () ? 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)


2 Answers

create "fonts" folder under "assets" folder. After that put your font file in "fonts" folder and write below code.

   Typeface tf =Typeface.createFromAsset(getAssets(),"fonts/YOURFONT.ttf");
   Paint paint = new Paint();
   paint.setTypeface(tf);
   canvas.drawText("Sample text in bold RECOGNITION",0,0,paint);
like image 155
Zankhna Avatar answered Sep 30 '22 03:09

Zankhna


Use this:

   Typeface tf = Typeface.createFromAsset(getAssets(),"RECOGNITION.ttf");
   Paint paint = new Paint();
   paint.setTypeface(tf);
   canvas.drawText("Sample text in bold RECOGNITION",0,0,paint);
like image 27
Shailendra Madda Avatar answered Sep 30 '22 02:09

Shailendra Madda