Hreza779
Hreza779

Reputation: 49

how to set Custom font for Widget?

i want to set custom font for TextView in widget,How?

 RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.weather_widget);
views.setTextViewText(R.id.txv_widget_location, "my text");

Upvotes: 1

Views: 202

Answers (1)

Anubhav Gupta
Anubhav Gupta

Reputation: 2000

Just replace xyz.ttf with your chosen font.

public Bitmap createBitmap(String texttoshow) 
{
  Bitmap myBitmap = Bitmap.createBitmap(160, 84, Bitmap.Config.ARGB_8888);
  Canvas myCanvas = new Canvas(myBitmap);
  Paint paint = new Paint();
  Typeface weatherwidget = Typeface.createFromAsset(this.getAssets(),"xyz.ttf");
  paint.setAntiAlias(true);
  paint.setSubpixelText(true);
  paint.setTypeface(weatherwidget);
  paint.setStyle(Paint.Style.FILL);
  paint.setColor(Color.WHITE);
  paint.setTextSize(65);
  paint.setTextAlign(Align.CENTER);
  myCanvas.drawText(texttoshow, 80, 60, paint);
  return myBitmap;
}

The above code applies the font and text to image view.

RemoteViews views = new RemoteViews(context.getPackageName(), 
 R.layout.weather_widget);
views.setImageViewBitmap(R.id.txv_widget_location, createBitmap("my_text"));

Upvotes: 1

Related Questions