1

I'm using text in canvas like this:

paintTextTime.setAntiAlias(true);
paintTextTime.setColor(Color.WHITE);
paintTextTime.setTextSize(60);
canvas.drawText(stringPlayTime, 220, 180, paintTextTime);

My question is how do I know which font I can use and how do I set the style of font to bold?

3D-kreativ
  • 9,053
  • 37
  • 102
  • 159

3 Answers3

3

You can easily change font by loading a new TypeFace. You can put any TTF font in your assets folder and load it using:

Typeface.createFromAsset(context.getAssets(), "myfont.ttf");

I would suggest that you cache the loaded font to prevent memory leaks on older Android versions:

public class FontCache {

    private static Hashtable<String, Typeface> fontCache = new Hashtable<String, Typeface>();

    public static Typeface get(String name, Context context) {
        Typeface tf = fontCache.get(name);
        if(tf == null) {
            try {
                tf = Typeface.createFromAsset(context.getAssets(), name);
            }
            catch (Exception e) {
                return null;
            }
            fontCache.put(name, tf);
        }
        return tf;
    }
}

And use it like this:

Typeface tf = FontCache.get(context, "myfont.ttf");
Paint paint = new Paint();
paint.setTypeface(tf);
canvas.drawText("Lorem ipsum", 0, 0, paint);
Overv
  • 8,433
  • 2
  • 40
  • 70
britzl
  • 10,132
  • 7
  • 41
  • 38
  • Thanks for the answer, but is there only one type of fontface available in the device or is there list och which fonts that are available? – 3D-kreativ Jun 06 '13 at 11:03
  • Have a look at this question: http://stackoverflow.com/questions/3532397/how-to-retrieve-a-list-of-available-installed-fonts-in-android – britzl Jun 06 '13 at 11:21
0

Google android typeface. This is the first result and is most likely what you are looking for.

Lunchbox
  • 1,538
  • 2
  • 16
  • 42
  • You can also download custom fonts if you feel like it or check out http://stackoverflow.com/questions/2888508/how-to-change-the-font-on-the-textview – Lunchbox Jun 06 '13 at 11:03
0

You can use the following code

Typeface tf = Typeface.create("Helvetica",Typeface.BOLD);

Paint paint = new Paint();

paint.setTypeface(tf);

canvas.drawText("Sample text in bold Helvetica",0,0,paint);
Shiva Saurabh
  • 1,281
  • 2
  • 25
  • 47
Manjunath
  • 123
  • 1
  • 1
  • 4