17

I would like to update my widget every time it gets resized. I figured out that this is done in:

onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions)

But I can't find out how to update the widget in it. I tried to redraw the widget by calling everything that I put in onUpdate(), but it's not working. How can I make use of the bundle?

1 Answers1

33

I would like to update my widget every time it gets resized.

Cool!

I figured out that this is done in onAppWidgetOptionsChanged()

More accurately, if you have the right actions in your <intent-filter>, on Android 4.1+ devices, you will find out about resize events via onAppWidgetOptionsChanged().

But I can't find out how to update the widget in it.

You update it the same way you update it in onUpdate(). Call updateAppWidget() on the AppWidgetManager with an appropriate RemoteViews.

I tried to redraw the widget by calling everything that I put in onUpdate(), but it's not working.

"it's not working" is not a particularly effective description of your symptoms.

How can I make use of the bundle?

For a Bundle named newOptions, you can find out your new size range via:

newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH)
newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH)
newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT)
newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT)

For example, this sample project contains an AppWidgetProvider that simply pours those values into a string and uses that to update a TextView. The result looks like:

Resize Widget, During Resize Operation

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Just FYI, this example doesn't work on my Galaxy S3 running build number JZ054K.I9300XXXELL4. However, it works beautifully on my Nexus 4. Thanks Mark! – Justin Apr 10 '13 at 17:12
  • My bad. onAppWidgetOptionsChanged() is not being called on resize. [Seems someone else is having this issue too](http://developer.samsung.com/forum/thread/galaxy-s3-412-does-not-handle-app-widget-resizing-correctly/77/225524?boardName=GeneralB&startId=zzzzz~&curPage=3) – Justin Apr 12 '13 at 15:57
  • @Justin: Oh, joy. I'll try to get my hands on an S3 to chime in on the issue. Thanks! – CommonsWare Apr 12 '13 at 16:09
  • How do you handle that situation on the S3 now? My Widget seems broken, because of a stupid Samsung bug. – AndreasEK May 13 '13 at 22:05
  • @AndreasEK I have found a solution: http://stackoverflow.com/questions/17396045/how-to-catch-widget-size-changes-on-devices-where-onappwidgetoptionschanged-not – frankish Jul 01 '13 at 00:23
  • @frankish thanks I will try that out! How did you know about that? It seems nowhere to be documented. – AndreasEK Jul 10 '13 at 08:23
  • @AndreasEK I logged every call to onReceive method. Be aware that this may not get called on other devices. So you may use it just to fill the holes for different devices. – frankish Jul 10 '13 at 08:49
  • So, I downloaded CommonsWare's app, and, when I place it on my home screen, the text does not immediately show up (onAppWidgetOptionsChanged() is not being called). I tried on stock 4.2 and stock 4.3. Any help? Also, I posted this as a question here: http://stackoverflow.com/questions/18526770/appwidgetprovider-onappwidgetoptionschanged-not-being-called-on-widget-placemen – Eric Cochran Aug 30 '13 at 15:55
  • @Justin lol, now that is odd. My collection-widget works fine on s3 but doesn't show scrollview on a nexus – bofredo Oct 17 '13 at 08:44
  • 6
    Is there an official link to show all available values for the bundle to have ? Also, how come we have min&max ? the size of the widget has one width and height... – android developer May 19 '15 at 07:09