I am creating a appwidget that consists of a single custom view called Foo.
xml/widget.xml:
I pretty much left my custom view intact, and implemented an ImageView for my widget, then rendered the view using the getDrawingCache()
MyView myView = new MyView(context);
myView.measure(150,150);
myView.layout(0,0,150,150);
myView.setDrawingCacheEnabled(true);
Bitmap bitmap=myView.getDrawingCache();
remoteViews.setImageViewBitmap(R.id.dial, bitmap);
You cannot have ANY custom view used in widget. In fact, even those android-predfined views are not all supported.
For detailed list of supported widgets/layouts, please read the documentation. Anything other than those documented cannot be placed in widgets.
Another way to do this without using getDrawingCache()
:
MyView myView = new MyView(this);
myView.measure(500, 500);
myView.layout(0, 0, 500, 500);
Bitmap bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
myView.draw(new Canvas(bitmap));
remoteViews.setImageViewBitmap(R.id.imageView, bitmap);
I used cache not to redraw all the view so I couldn't use the code above. And I find it more elegant. I hope it could be useful to someone.
see the documentation.
AnalogClock, Button, Chronometer, ImageButton, ImageView, ProgressBar and TextView are the supported views. For layouts you have to use FrameLayout, LinearLayout or RelativeLayout.