Vineet Shukla
Vineet Shukla

Reputation: 24031

Updating widget width - height programmatically on device rotation

I am working over widget where I need to control the width and height of widget programmatically when device rotates from portrait to landscape and landscape to protrait. For this when configuration change I call the below code to update the widget width programmatically:

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 this, I get call onAppWidgetOptionsChanged() with new values but widget don't get resized.

However I am also calling onUpdate().

minSdkVersion is 16.

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

Thanks

Upvotes: 5

Views: 2491

Answers (1)

Gabriel H
Gabriel H

Reputation: 1576

ok here is a workaround that i think will work:

create 2 different layouts (widgetHorizon,widgetVertical).

on time of configuration change (and i assume that you are catching it correctlly)

you send an update intent as follows:

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

in the on update method: check the configuration and accordingly call:

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

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

that way the layout will be chosen according to the current status.

Upvotes: 3

Related Questions