tags:

views:

117

answers:

1

I am writing a home screen widget. Is it possible to add a View, e.g. ImageView, to a home screen widget through RemoteViews? I want to generate the views for the home screen widget dynamically.

Thanks.

            updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_news);

            updateViews.setTextViewText(R.id.widget_title, mTitle);

            updateViews.setImageViewBitmap(R.id.widget_picture, 
                        BitmapFactory.decodeByteArray(image, 0, image.length));

My code is like above. But we I call updateViews.addView(aView), my IDE didn't allow me to do that and give me an compile error.

A: 

Every time you use RemoteViews, you are pushing the definition of your UI to the app widget. You want more Views? Just use an appropriate layout at that point when creating the RemoteViews instance.

CommonsWare
I edited my post by adding some example code. I tried to call updateViews.addView(aView), but failed. Any thoughts?
As I wrote, "Just use an appropriate layout at that point when creating the RemoteViews instance". You will notice how your update code calls new RemoteViews() and supplies a layout. If you want a different layout for the app widget, switch to using a different layout file.
CommonsWare
What if I want to dynamically generate the views in RemoteViews? For example, I read pictures from a database. The first time, I retrieve 3 pictures, so I create 3 ImageViews in the RemoteViews. The next time, I retrieve 5 pictures, then I create 5 ImageViews in the RemoteViews of the widget. Is it possible? Thanks.
I am with you, kknight. Dynamically would be the way to go.
steff