Widget width update - height programmatically when the device rotates

I am working on widgets where I need to programmatically control the width and height of a widget when the device rotates from portrait to landscape and landscape to rub. To do this, when changing the configuration, I call the code below to programmatically update the width of the widget:

for (int id : appWidgetIds){
    Bundle newOptions = appWidgetManager.getAppWidgetOptions(id);
    int minWidth = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, 0);
    newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth - 100);
    appWidgetManager.updateAppWidgetOptions(id, newOptions);
}

After that, I get a call to onAppWidgetOptionsChanged () with the new values, but the widget does not change.

However, I also call onUpdate () .

minSdkVersion - 16.

I searched a lot, but could not find related to this problem, thanks if in advance for your precious time.

thank

+4
source share
1

ok , , , :

2 (widgetHorizon, widgetVertical).

( , )

:

private void sendUpdateBroadcastToWidget() {
        Intent intent = new Intent(this,WidgetProvider.class);
        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);

        // Use an array and EXTRA_APPWIDGET_IDS instead of AppWidgetManager.EXTRA_APPWIDGET_ID,
        // since it seems the onUpdate() is only fired on that:
        int[] ids = {R.xml.quick_actions_widget};
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,ids);
        sendBroadcast(intent);
    }

on update: , , :

RemoteViews views = new RemoteViews(getPackageName(),R.Layout.widgetHorizon)

RemoteViews views = new RemoteViews(getPackageName(),R.Layout.widgetVertical)
appWidgetManager.updateAppWidget(appWidgetId, views);

.

+3

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


All Articles