I am building a widget for my Android application that will have several buttons on it. Based on the size of the widget, I want the amount of buttons on the widget to grow and shrink. This is what it currently looks like in a 4x1 view:
And when you shrink it down to 3x1:
Is there anyway to dynamically hide the 4th button in the 3x1 view so the other 3 buttons do not get squished? Same would go for a 2x1 or a 1x1. Any input on this would be greatly appreciated!
Thanks!
As was suggested, you have no other way than to implement onAppWidgetOptionsChanged
in your widget provider - API level < 16 users won't have this feature unfortunately.
In order to hide the buttons when the widget shrinks, your widget needs to behave similarly to the weather widget given as an example in the Android Design site, under pattern "Widgets" (appwidgets actually). You show fewer buttons as the widget shrinks, more buttons as it grows, setting the buttons' visibility.
In onAppWidgetOptionsChanged
, using the values given by the newOptions Bundle, you must detect the variation in width and height and set size buckets as advised in the docs (from the same Design page):
In fact, as the user resizes a widget, the system will respond with a dp size range in which your widget can redraw itself. Planning your widget resizing strategy across "size buckets" rather than variable grid dimensions will give you the most reliable results.
What is really challenging is how to determine those buckets. The newOptions Bundle have min_width, min_height, max_width and max_height values that differ substantially among devices, screen densities, launchers, etc. Unfortunately the Android team did not show us how to do this, and there are few code samples of onAppWidgetOptionsChanged
in the web, most of them very simple.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With