Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 769
Vineet Shukla Avatar asked Oct 20 '22 19:10

Vineet Shukla


1 Answers

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.

like image 112
Gabriel H Avatar answered Oct 22 '22 10:10

Gabriel H