0

Hi and thanks for your help.

I need to use my custom font in my App Widget.

Normally I would use something like:

TextView txt = (TextView) findViewById(R.id.custom_font);  
Typeface font = Typeface.createFromAsset(getAssets(), "myfont.ttf");  
txt.setTypeface(font);

But In cannot call setTypeface(font) on RemoteViews.

Please any suggestion?

Thanks!

Lisa Anne
  • 4,482
  • 17
  • 83
  • 157

2 Answers2

3

You can use the following method.

Render the font onto a canvas, and then pass it on to a bitmap and assign that to an ImageView.

public Bitmap buildUpdate(String text) 
{
    Bitmap myBitmap = Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_4444);
    Canvas myCanvas = new Canvas(myBitmap);
    Paint paint = new Paint();
    Typeface mytypeface = Typeface.createFromAsset(this.getAssets(),"fontname.ttf");
    paint.setAntiAlias(true);
    paint.setSubpixelText(true);
    paint.setTypeface(clock);
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);
    paint.setTextSize(65);
    paint.setTextAlign(Align.CENTER);
    myCanvas.drawText(text, 80, 60, paint);
    return myBitmap;
}

use it like:

String text = "This is my text";
RemoteViews views = new RemoteViews(getPackageName(), R.layout.my_widget_layout);
views.setImageViewBitmap(R.id.my+imageview, buildUpdate(text));

Hope this will help :)

Vettiyanakan
  • 7,957
  • 6
  • 37
  • 55
-1

You can't do this as the app runs in a different process so you can only use system fonts.

(I know the user posted this ages ago, but the answer may be useful to others that come across it)

Andrew
  • 7,548
  • 7
  • 50
  • 72