RemoteViews setViewVisibility on Android Widget

trigger picture trigger · Feb 6, 2012 · Viewed 9.6k times · Source

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!

Answer

AndroidKen picture AndroidKen · Feb 8, 2012

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);