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?