4

Is there a way to change the font in a widget? I've seen some questions/answers on here but some say it can't be done. Others say it can be done but then don't provide much information. I'm talking about a custom font that I put in my assets folder. The font I want to use is Roboto from the design guidelines.

I mostly want to ask because of the other questions stating it isn't possible. I have the Google Currents widget on my Nexus 7 that uses the Roboto font on it's widget so surely it is possible?

The TextView I want to change is well, just a textview. It has no formatting/styles on it at the moment. The only thing I really need is the font changed to Roboto light.

I would appreciate some advice on how to do this. Thanks.

I actually plan on open sourcing the widget I am making here so it could help others too.

Ayush
  • 3,989
  • 1
  • 26
  • 34
RED_
  • 2,997
  • 4
  • 40
  • 59

1 Answers1

9

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);
Ahmad
  • 69,608
  • 17
  • 111
  • 137
  • what about custom fonts. I download custom.ttf store it in assests folder use the same for test view – Raghunandan May 05 '13 at 18:58
  • Custom fonts. http://stackoverflow.com/questions/5634245/how-to-add-external-fonts-to-android-application – Michal Palczewski May 05 '13 at 18:59
  • http://stackoverflow.com/questions/15991521/custom-font-in-android-for-whole-application/15991573#15991573 – Raghunandan May 05 '13 at 19:00
  • @Raghunandan OP is talking about custom fonts in AppWidgets. – Ahmad May 05 '13 at 19:00
  • @Ahmad ok. If its appwidgets we can't use custom fonts? – Raghunandan May 05 '13 at 19:02
  • @MichalPalczewski You can't set custom fonts in AppWidgets. – Ahmad May 05 '13 at 19:02
  • 1
    @Ahmad thanks for the clarification. Well then he can use available system fonts right? – Raghunandan May 05 '13 at 19:03
  • @Ahmad 4.1+ only? Damn... That's a shame. Just seen your edit. I presume the bitmap thing will only work if you have the text already set out though? Hence the "Hello world" part? Mine changes based on a database so I guess I should stick with the first bit – RED_ May 05 '13 at 19:03
  • You can of course load your text from a database and then rebuild the bitmap, but this approach with bitmaps can get unnecessarily complicated, so yes, I would stick with the `setTextViewText(...)` approach. – Ahmad May 05 '13 at 19:05
  • 1
    @Ahmad Yeah fair enough. I appreciate the help! +1 and Marked as answer. – RED_ May 05 '13 at 19:08