Faced the situation when it was necessary in the widget, on the screen of the andphone, to display the text in a beautiful non-standard font. From that moment this post began.
Developing a widget is a little different from developing an
activity , and this “little” sometimes puts a stick in the wheel. I will not describe in detail the development of the widget, on
Habré there are already some wonderful posts on this topic (for example,
Hello World widget for Android or even such a cheat sheet,
Creating a Widget ), I’ll only focus on the features.
The essence of the problem
The problem was that we can write a custom font in
activity by setting just the
Typeface property of the
View we need.
')
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Aliner.ttf"); TextView tv = (TextView) findViewById(R.id.txtFont); tv.setTypeface(tf);
But with the widget is no longer pass. To get to the
View we can only through the object
RemoteViews , which provides us with not very great opportunities. We too will not easily get to the “views”, although we can change the meanings of some, through the methods:
remoteView.setInt(R.id.widgetPNG, "setAlpha", 50); remoteView.setBoolean(R.id.a_text_view, "setSelected", true); remoteView.setCharSequence(viewId, "setText", "Hello World!"); .....
but the question with the font remains open.
The solution was found using
Bitmap 's.
Connect custom font
The first thing that is needed is to find a suitable font and put it in the directory
./assets/fonts/ :

Then it remains only to connect it in the widget code:
Typeface tf = Typeface.createFromAsset(context.getAssets(),"fonts/Benegraphic.ttf");
Text to picture
As I said, we will work with
Bitmap 'om. Having the text, the following method will help us convert it into a picture:
private Bitmap convertToImg(String text, Context context) { Bitmap btmText = Bitmap.createBitmap(400, 100, Bitmap.Config.ARGB_4444); Canvas cnvText = new Canvas(btmText); Typeface tf = Typeface.createFromAsset(context.getAssets(),"fonts/Benegraphic.ttf"); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setSubpixelText(true); paint.setTypeface(tf); paint.setColor(Color.WHITE); paint.setTextSize(50); cnvText.drawText(text, 150, 50, paint); return btmText; }
The
convertToImg () method returns a ready-made bitmap with text written in
Benegraphic.ttf . We just have to install it in the widget.
Adding "text" to the widget
Installing the generated
bitmap into the work widget will not make much of it; it’s just 3 lines of code:
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget); remoteViews.setImageViewBitmap(R.id.imText, convertToImg("Hello World!", context)); appWidgetManager.updateAppWidget(appWidgetIds, remoteViews); }
At this all coding ends. Running the application and installing the widget on one of the desktops, we will see a similar picture:

So we got a custom font widget
Android .
Conclusion
One way, we all solved the problem. It certainly was a way out, but I wanted something more native. I really hope that there are more ways, but alas, neither come up nor find anything else could not. People on the Internet also ask this question, but have not yet seen the answer. Maybe some of you have already encountered, it would be interesting to hear in the comments :)
Download the entire project
here .