Resize Android widget

I am creating a widget for my Android application that will have several buttons. Based on the size of the widget, I want the number of buttons on the widget to increase and decrease. This is what the 4x1 view currently looks like: 4x1 widget

And when you reduce it to 3x1: 3x1

In any case, in order to dynamically hide the 4th button in the 3x1 view so that the other 3 buttons are not compressed? The same can be done for 2x1 or 1x1. Any input on this would be greatly appreciated!

Thanks!

+6
source share
4 answers

As suggested, you have no other way but to implement onAppWidgetOptionsChanged in your widget provider - API level <Unfortunately, 16 users will not have this function.

To hide the buttons when reducing the widget, your widget should behave similarly to the weather widget, shown as an example on the Android Design website, according to the " Widgets " template (in fact, the application). You show fewer buttons when the widget is compressed, the number of buttons increases as you increase, setting the visibility of the buttons.

enter image description here

In onAppWidgetOptionsChanged , using the values ​​specified by the newOptions Bundle package, you must determine the change in width and height and set the dimensional buckets as indicated in the documents (from the same design page):

In fact, when the user resizes the widget, the system will respond with dp in which your widget can redraw itself. Planning a strategy for resizing a widget by "dimension buckets" rather than a variable grid size will give you the most reliable results.

What is really difficult is to identify these buckets. The newOptions package has the values ​​min_width, min_height, max_width and max_height, which differ significantly between devices, screen density, launchers, etc. Unfortunately, the Android team did not show us how to do this, and there are several onAppWidgetOptionsChanged code onAppWidgetOptionsChanged on the web, most of them are very simple.

+8
source

As they said. You can use onAppWidgetOptionsChanged

So my suggestion is to

1) Make different layouts. With 3 and 4 buttons.

2) Change the layout base to the number of columns in the widget.

 @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) { // See the dimensions and Bundle options = appWidgetManager.getAppWidgetOptions(appWidgetId); // Get min width and height. int minWidth = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH); int minHeight = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT); // Obtain appropriate widget and update it. appWidgetManager.updateAppWidget(appWidgetId, getRemoteViews(context, minWidth, minHeight)); super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions); } /** * Determine appropriate view based on row or column provided. * * @param minWidth * @param minHeight * @return */ private RemoteViews getRemoteViews(Context context, int minWidth, int minHeight) { // First find out rows and columns based on width provided. int rows = getCellsForSize(minHeight); int columns = getCellsForSize(minWidth); // Now you changing layout base on you column count // In this code from 1 column to 4 // you can make code for more columns on your own. switch (column) { case 1: return new RemoteViews(context.getPackageName(), R.layout.activity_layout_widget_1column); case 2: return new RemoteViews(context.getPackageName(), R.layout.activity_layout_widget_2column); case 3: return new RemoteViews(context.getPackageName(), R.layout.activity_layout_widget_3column); case 4: return new RemoteViews(context.getPackageName(), R.layout.activity_layout_widget_4column); default: return new RemoteViews(context.getPackageName(), R.layout.activity_layout_widget_4column); } } /** * Returns number of cells needed for given size of the widget. * * @param size Widget size in dp. * @return Size in number of cells. */ private static int getCellsForSize(int size) { int n = 2; while (70 * n - 30 < size) { ++n; } return n - 1; } 
+8
source

In version 4.1 (api level 16), a new function getAppWidgetOptions () appeared, which returns a Bundle with some calibration parameters. See here . Perhaps you can use this for what you need, but afaik does not have a similar option for versions below 4.1.

+4
source

You can try listening to the onAppWidgetOptionsChanged callback: it should fire when the layout is resized. However, I really did not see (right now) to determine the actual amount of screen real estate that the widget occupies, so there is no good way to change the visibility of buttons based on size.

0
source

Source: https://habr.com/ru/post/947419/


All Articles