Tikiboy
Tikiboy

Reputation: 912

Sizing Text Bitmap for Android Home Screen Widget

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?

Upvotes: 0

Views: 313

Answers (1)

user3811037
user3811037

Reputation:

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."

Upvotes: 1

Related Questions