As it became known - in the new version of Android Jelly Bean (API v16), the ability to resize the widget appeared, and not just changes, but automatic, which now adjusts to the free space on the screen.
The function is convenient and useful, but there is almost nothing in the official documentation about it, which is rather strange.

Since I am developing several widgets, I decided to add this functionality.
The first thing I noticed is that the widget does not change the size at all, it was decided to simply add 3 lines to widget_provider.xml (res / xml):
android:resizeMode="horizontal|vertical" android:minResizeHeight="72dip" android:minResizeWidth="72dip"
android: resizeMode is responsible for the direction of resizing the widget and has 3 possible values:
- horizontal - allows you to stretch the widget horizontally
- vertical - allows you to stretch the widget vertically
- none - forbids stretching the widget (by default)
android: minResizeHeight and
android: minResizeWidth are responsible for the minimum size of the widget that can be set.
In the code above, the widget can be stretched in any direction and its minimum size is 1 * 1 cell.
')
Everything seemed to be fine, and everything works, however how large the widget has become is not clear.
In the documentation, I found a mention of the
onAppWidgetExtrasChanged method
(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newExtras) , which should be called when resized, but I never called it.
Then it turned out that when the widget is resized, a new Broadcast "
android.appwidget.action.APPWIDGET_UPDATE_OPTIONS " is sent. Following the logic, it was possible to assume that the new size should go with it. So it turned out.
But it does not go in a very explicit form. The result is the following code:
final String action = intent.getAction(); if (action.equalsIgnoreCase("android.appwidget.action.APPWIDGET_UPDATE_OPTIONS")) { Bundle b = intent.getBundleExtra("appWidgetOptions"); int appWidgetMinWidth = (Integer) b.get("appWidgetMinWidth")/80;
4 sizes are sent - minimum and maximum width and height. During the experiment, it became clear that these dimensions are constant and more convenient to focus on the minimum width and maximum height.
When the widget is resized, its size in the number of cells will be displayed in the log. Then this size can be saved for this widget (its ID: int id = intent.getIntExtra (AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);) and when updating, read these data and adjust the parameters.
In the earlier version of Android, there was naturally no such function, and many developers, including myself, made several widget sizes in order to give people a choice, now this choice is not needed, so for good, leave the user with Jelly Bean 1 Widget and scaling function, and users of earlier versions have several widgets with different sizes, but all this would be in 1 package.
it turned out to be very simple to implement:
Create a file bool.xml with contents in the res / values folder
<resources> <bool name="v16">true</bool> </resources>
And in the res / values-v16 folder the same file, but several different contents:
<resources> <bool name="v16">false</bool> </resources>
After that, in AndroidManifest.xml, at the receivers of widgets that need to be hidden in the new version we add
android:enabled="@bool/v16"
And everything, starting with the v16 API, these widgets will be disabled and will not be displayed in the list.
PS This method was tested on MDPI and HDPI screens with a launcher grid size of 4 * 4, please ask Android tablet users with a grid size larger than 4 * 4 to check the method, perhaps the sizes will change in a slightly different sequence.