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

Since I am developing several widgets, I decided to add this functionality.
The first thing I noticed was that the widget does not change size at all, this was decided simply by adding 3 lines to widget_provider.xml (res / xml):
android: resizeMode is responsible for the direction in which the widget is resized and has 3 possible values:
android: minResizeHeight and android: minResizeWidth are responsible for the minimum widget size that can be set.
In the code above, the widget can be stretched in any direction and its minimum size is 1 * 1 cell.
It seems that everything has become excellent, and everything works, but what size the widget has become is not clear.
I found a mention in the documentation of the onAppWidgetExtrasChanged method (Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newExtras) , which should be called when resizing, but I never called it.
Then it turned out that when the widget was resized, a new Broadcast " android.appwidget.action.APPWIDGET_UPDATE_OPTIONS". Following the logic, it could be assumed that a new size should go with it. It turned out that way.
But it is not sent in a very explicit form. As a result, we got the following code:
4 sizes are sent - minimum and maximum width and height. During the experiment, it turned out that these dimensions are constant and more convenient to focus on the minimum width and maximum height.
When changing the size of the widget, its size in the number of cells will be displayed in the log. Further, 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 this data and adjust the parameters.
In earlier versions of Android, of course, there was 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 zoom 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 bool.xml file with the contents in the res / values folder
And in the res / values-v16 folder there is the same file, but several other contents:
Then in AndroidManifest.xml we add the widgets receivers that need to be hidden in the new version
And everything, starting with API v16, these widgets will be disabled and not displayed in the list.
PS this method was tested on MDPI and HDPI screens with the size of the grid of the launcher 4 * 4, the request of users of tablets with Android Jelly Bean and the size of the grid larger than 4 * 4 to check the method, maybe the sizes will change in a slightly different sequence.
The function is convenient and useful, but in the official documentation about it there is almost nothing, which is rather strange.

Since I am developing several widgets, I decided to add this functionality.
The first thing I noticed was that the widget does not change size at all, this was decided simply by adding 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 in which the widget is resized 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 widget size that can be set.
In the code above, the widget can be stretched in any direction and its minimum size is 1 * 1 cell.
It seems that everything has become excellent, and everything works, but what size the widget has become is not clear.
I found a mention in the documentation of the onAppWidgetExtrasChanged method (Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newExtras) , which should be called when resizing, but I never called it.
Then it turned out that when the widget was resized, a new Broadcast " android.appwidget.action.APPWIDGET_UPDATE_OPTIONS". Following the logic, it could be assumed that a new size should go with it. It turned out that way.
But it is not sent in a very explicit form. As a result, we got 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;
// int appWidgetMaxWidth = (Integer) b.get("appWidgetMaxWidth");
// int appWidgetMinHeight = (Integer) b.get("appWidgetMinHeight");
int appWidgetMaxHeight = (Integer) b.get("appWidgetMaxHeight")/100;
Log.i("Widget","Width="+appWidgetMinWidth+" Height="+appWidgetMaxHeight);
4 sizes are sent - minimum and maximum width and height. During the experiment, it turned out that these dimensions are constant and more convenient to focus on the minimum width and maximum height.
When changing the size of the widget, its size in the number of cells will be displayed in the log. Further, 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 this data and adjust the parameters.
In earlier versions of Android, of course, there was 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 zoom 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 bool.xml file with the contents in the res / values folder
true
And in the res / values-v16 folder there is the same file, but several other contents:
false
Then in AndroidManifest.xml we add the widgets receivers that need to be hidden in the new version
android:enabled="@bool/v16"
And everything, starting with API v16, these widgets will be disabled and not displayed in the list.
PS this method was tested on MDPI and HDPI screens with the size of the grid of the launcher 4 * 4, the request of users of tablets with Android Jelly Bean and the size of the grid larger than 4 * 4 to check the method, maybe the sizes will change in a slightly different sequence.