RemoteViews setViewVisibility on Android Widget
Asked Answered
C

2

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!

Cur answered 5/2, 2012 at 23:52 Comment(0)
T
17

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);
Torgerson answered 8/2, 2012 at 3:39 Comment(1)
Tried both but none is working. Are you sure INVISIBLE works?Cyanic
A
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.

Agnella answered 5/12, 2014 at 4:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.