5

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

Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63

1 Answers1

3

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.

Gabriel H
  • 1,558
  • 2
  • 14
  • 35
  • Plus 1, yes, this is a workaround but I am looking why the api call is not working when it is there in api. – Vineet Shukla Jan 02 '15 at 15:55
  • @VineetShukla i think it might be because your layout matches a certain size, you try to update a variable called minWidth, meaning if your layout is bigger than that it might stays bigger, if you actually want to resize it i think resizing the layout as well might be your only option – Gabriel H Jan 04 '15 at 09:51