0

I would like to create a home screen widget that uses a custom font to display a paragraph of text.

Right now, the method I am using the draw the text is by inflating a layout used by the text, then I set the text and finally convert that layout into a bitmap. The code is as follows:

//Leveraging view framework to built my text image
View v = LayoutInflater.from(context).inflate(R.layout.item_widget_text, null);
((TextView)v.findViewById(R.id.title)).setText(title);
((TextView)v.findViewById(R.id.subtitle)).setText(subtitle);
v.measure(MeasureSpec.makeMeasureSpec(1040, MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(),v.getMeasuredHeight());

//Creating the bitmap
final Bitmap returnedBitmap = Bitmap.createBitmap(v.getMeasuredWidth(),
            v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
v.draw(canvas);

In order to create the bitmap for the layout to be painted on, I need to know the size of the widget, in particular, the widget's width.

My problem here is that I have no idea how to get the widget's size. For android 4.1 and above, I am using onAppWidgetOptionsChanged(). However, my app supports 4.0 and above. So for android versions below 4.1, I am unable to size my layout properly, thus I cannot generate the correct bitmap.

Is there any way to do this? Or is there a more elegant/correct approach that I am just now seeing?

I see many widgets on a 4.0 device, like the Gmail widget. How are they able to draw their widget so beautifully? Are they using custom fonts or are they just using standard textview with no typeface?

Tikiboy
  • 892
  • 10
  • 20
  • Don't see any answers yet. Thought I'd throw this out... http://stackoverflow.com/questions/14062651/how-to-use-onappwidgetoptionschanged-in-a-widget –  Jul 07 '14 at 04:43
  • @user3811037 Thanks for your answer! However, the post only answers the issue of sizing for devices 4.1+. My problem is with devices below 4.1. I am looking for either a way to get the size of the widget or a more elegant way of achieving my goal (using custom fonts on a dynamically size-able widget). – Tikiboy Jul 07 '14 at 05:27

1 Answers1

1

Not sure if this will help either, but... http://www.vogella.com/tutorials/AndroidWidgets/article.html#overview_size

"2.3. Widget size Before Android 3.1 a widget always took a fixed amount of cells on the home screen. A cell is usually used to display the icon of one application. As a calculation rule you should define the size of the widget with the formula: ((Number of columns / rows) * 74) - 2. These are device independent pixels and the -2 is used to avoid rounding errors.

As of Android 3.1 a widget can be flexible in size, e.g., the user can make it larger or smaller. To enable this for widget, you can use the android:resizeMode="horizontal|vertical" attribute in the XML configuration file for the widget."

  • Hey there, thanks for the answer, but unfortunately this method isnt effective either. The widget will never tell us how many cells it is currently taking up (You could infer roughly for android 3.1 and above). Thus without that information, sizing accurately is also not possible. :( – Tikiboy Jul 14 '14 at 05:45
  • Hey again. Did you figure this out yet? Found info about 1.5 SDK platform widgets with specs. http://android-developers.blogspot.com/2009/04/introducing-home-screen-widgets-and.html And more info here: http://android-developers.blogspot.com/2009/04/ui-framework-changes-in-android-15.html –  Jul 20 '14 at 21:30
  • Yo man, wow! Thanks for the continuous help. I kinda concluded that its not entirely possible and resorted to using normal textviews and normal typefaces instead. Ill look into those links and if they result in an answer, ill update this thread with a functional solution. Thanks alot for your help once again! :D – Tikiboy Jul 21 '14 at 04:44