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.
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.
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.
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.
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);
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)
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With