How to Use a Custom Typeface in a Widget

How to use a custom typeface in a widget?

What is needed is to render the font onto a canvas, and then pass it on to a bitmap and assign that to an ImageView. Like so:

public Bitmap buildUpdate(String time) 
{
Bitmap myBitmap = Bitmap.createBitmap(160, 84, Bitmap.Config.ARGB_4444);
Canvas myCanvas = new Canvas(myBitmap);
Paint paint = new Paint();
Typeface clock = Typeface.createFromAsset(this.getAssets(),"Clockopia.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(time, 80, 60, paint);
return myBitmap;
}

That's the part doing the font to image thingie, and this is how to use it:

String time = (String) DateFormat.format(mTimeFormat, mCalendar);
RemoteViews views = new RemoteViews(getPackageName(), R.layout.main);
views.setImageViewBitmap(R.id.TimeView, buildUpdate(time));

As you might notice, this code just shows the current time in the imageview, but it can easily be adjusted to whatever needs.

Edit:

ARGB_4444 is deprecated for ARGB_8888 as stated in the documentation

This field was deprecated in API level 13. Because of the poor quality of this configuration, it is advised to use ARGB_8888 instead.

how to apply custom font in all of widgets in android (actionbar,drawer and any text) without using any third party library?

Till now,

There is no way to apply global font to all the TextView throughout whole App using Android APIs, though Android O introduces a new feature, Fonts in XML, which lets you use fonts as resources.

Refer this link to Learn more

If we talk about libraries, there is most famous that can do the trick for you. Check this out - Calligraphy. Github page have better explanation about how you use and how things works behind.

Custom fonts for home screen widget TextView in android

Have you seen this link yet?

What is needed is to render the font onto a canvas, and then pass it
on to a bitmap and assign that to an imageview.

How to use a custom typeface in a widget?

By the way, you've asked 18 questions and have not accepted a single answer. I suggest you start accepting some or people will be less inclined to help you.

Custom font iOS in Today Widget always return nil

Maybe it's because you forgot to add key in your .plist file.

Add the key Fonts provided by application to a new row. Add items for each font you have added.

Changing the font of a textview within a widget?


Is there a way to change the font in a widget?

Yes, but unfortunately you can only use the system fonts. You can use the Roboto fonts natively on Android 4.1+ like this though:

android:fontFamily="sans-serif"           // roboto regular
android:fontFamily="sans-serif-light" // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed

The only way to use Roboto on previous versions for your Text would be to create a Bitmap and draw your Text on Canvas like this:

    Bitmap myBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas myCanvas = new Canvas(myBitmap);
Paint paint = new Paint();
Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/myFont.ttf");

paint.setTypeface(font);
paint.setStyle(Paint.Style.FILL);
paint.setColor(...);
paint.setTextSize(...);
myCanvas.drawText("Hello World", x, y, paint);

And then set it like this to your RemoteView:

rViews.setImageViewBitmap(R.id.myImageView, myBitmap);


Related Topics



Leave a reply



Submit