1

I have a Widget that needs to be populated with dynamic content.

Specifically I'd like it to contain a ViewFlipper whose Views are updated at runtime.

I'm not clear on how to access the ViewFlipper from the onUpdate method of my AppWidgetProvider (assuming this is the correct place to affect change on the Widget and all the examples I'm finding on-line deal with static layouts that have all their elements defined in the xml).

So...

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {
    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

    //in pseudo code I'm looking for something that lets me do this...
    **(ViewFlipper) vf = remoteViews.findViewById(R.id.myFlipper);**

//EDIT:
//based on commonsware's suggestion... I'm here...
ImageView stationView = (ImageView)((Activity)context).getLayoutInflater().inflate(R.layout.widget_station, null);

//how what?

    pushWidgetUpdate(context, remoteViews);

}

However, RemoteViews doesn't have such a method, so, I'm perplexed as to how to handle this. I see according to the docs that Widgets support ViewFlippers... but do they not support populating them dynamically?

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236

1 Answers1

1

Specifically I'd like it to contain a ViewFlipper whose Views are updated at runtime

There are two possible interpretations of this request.

One is that you want the ViewFlipper to hold a fixed number of "pages", and each "page" always has the same widgets, but you want to populate the contents of those widgets. In that case, you populate them the same way that you do anything else in an app widget: by referring to those specific widgets, ignoring the ViewFlipper.

Or, you want a ViewFlipper that has varying number of pages at different times, or where the pages have varying layouts. In that case, you can try addView() for each page, specifying the ViewFlipper id as the first parameter. Or, try AdapterViewFlipper, and see if setRemoteAdapter() will cause the app widget to reload its contents from the RemoteViewsService.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I love the sound of the truly dynamic solution, but am unclear on the addView() solution. I have always used addView as `myFlipper.addView(newFlipperPage)`. And my question is *precisely the "how do I get a reference to `myFlipper`" in the Provider. – Yevgeny Simkin Mar 22 '14 at 19:30
  • so... I've edited my question to get as far as I can get based on how I understand your answer. Would you be so kind as to elaborate how I can get this inflated View attached to the ViewFlipper? – Yevgeny Simkin Mar 22 '14 at 19:42
  • @GeniaS.: `addView()` on `RemoteViews` takes a `RemoteViews` as the second parameter. Use the `RemoteViews` constructor to create a `RemoteViews` instance, the way you did for the `ViewFlipper` itself. – CommonsWare Mar 22 '14 at 20:39
  • one of us is misunderstanding the other (I'm going to assume it's me doing the misunderstanding). I don't have an instance of the "ViewFlipper itself". That's the crux of my question... *how do I get a reference to my ViewFlipper so as to be able to add views to it? Or do you mean that I should create the ViewFlipper as just another dynamic View and then add it to the RemoteViews? Currently my ViewFlipper is in the layout xml... it's *this layout xml ViewFlipper that I'm trying to append Views to... but I'm getting the sense that you're suggesting I should just generate a new one? – Yevgeny Simkin Mar 22 '14 at 22:09
  • ok... this answer http://stackoverflow.com/questions/4078039/remoteview-addview-not-working in conjunction with your advice works perfectly. Thanks very much.! – Yevgeny Simkin Mar 22 '14 at 22:40