RemoteViews setViewVisibility for Android widget

In my AppWidgetProvider application, I do the 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 am updating the layout through updateAppWidget, this change does not affect the user interface. What could be wrong? Many thanks!

+4
source share
2 answers

Have you tried using INVISIBLE rather than GONE? Gone will remove the view as if it had never been there. And the invisible holds the viewing location in the layout, but makes it invisible.

  rv.setViewVisibility(R.id.page1, View. INVISIBLE); 
+8
source

I have the same problem with the widget application. I found that appWidgetId is wrong if you do this. When you first update the user interface, you must keep track of the appWidgetId static variable. And then we can use this variable as a regular appWidgetId to update the interface.

Or you can try the code below in onReceiver :

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

which represent RemoteViews .

Thank you, sorry for my English.

+1
source

Source: https://habr.com/ru/post/1394927/


All Articles