9

In my AppWidgetProvider, I do following:

@Override
public void onReceive(Context ctx, Intent intent) {
    final String action = intent.getAction();
    if (action.equals(NEXTPAGE_ACTION)) {
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(ctx);
        RemoteViews rv = new RemoteViews(ctx.getPackageName(), R.layout.widget_layout);
        rv.setViewVisibility(R.id.page1, View.GONE);
        rv.setViewVisibility(R.id.page2, View.VISIBLE);
    final int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
    appWidgetManager.updateAppWidget(appWidgetId, rv);
    }
}

Although I'm updating the layout via updateAppWidget, the change is not really reflected on the UI. What could be possibly going wrong? Thanks much!

trigger
  • 141
  • 1
  • 2
  • 3

2 Answers2

16

Have you tried using INVISIBLE and not GONE? Gone will remove the view as if it were never there. And invisible will hold the view's place in the layout, but make it invisible.

 rv.setViewVisibility(R.id.page1, View. INVISIBLE);
AndroidKen
  • 215
  • 1
  • 3
  • 13
1

I have the same problem with widget app. I detect appWidgetId wrong if you get like that. When the first time update UI, you must track appWidgetId to a static variable. And then, we can use that variable as common appWidgetId for update UI.

Or you can try code below in onReceiver function:

final ComponentName provider = new ComponentName(context, this.getClass()); appWidgetManager.updateAppWidget(provider, views);

which views is RemoteViews.

Thanks, sorry about my English.

Ahmed
  • 651
  • 17
  • 42
taibt
  • 11
  • 1