8

I cannot figure out or find a solution by googling for this problem. I have an android app with an appwidget, looks like http://www.livescorewidget.eu/img/screendumps/widget.png, and I am adding the data rows on the fly. Due to different devices the height of the widget is different and therefore a different amount of space is available for my rows. I want to know how many dips or pixels my widget uses so I can calculate how many rows there is room for. Is it possible?

Even better could be if you could calculate how much height available in the layout for the rows.

Thanks

user971769
  • 117
  • 2
  • 6

1 Answers1

26

From Jelly Bean onwards you can get a Bundle object containing widget dimensions using the getAppWidgetOptions() method in AppWidgetManager:

Bundle options = appWidgetManager.getAppWidgetOptions(widgetId);

int minWidth = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
int maxWidth = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH);

int minHeight = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
int maxHeight = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT);

minWidth and maxHeight are the dimensions of your widget when the device is in portrait orientation, maxWidth and minHeight are the dimensions when the device is in landscape orientation. All in dp.

Simes
  • 453
  • 5
  • 8
  • 5
    Unfortunately both LG and Samsung launcher report incorrect values with this API. – Nathan Schwermann Jul 03 '14 at 19:41
  • Yes, from Jelly Bean onwards, as I said. – Simes Mar 24 '15 at 13:38
  • 3
    Have any one had a wrong size problem on Huawei devices when using this method? It seems I get the min widget provider size instead of real soze. – RusArtM Jan 14 '16 at 04:53
  • 1
    Those constants, I've been searching for days for some helpful explanation. The documentation says, "A bundle extra that contains the lower bound on the current width, in dips, of a widget instance." which gives nothing about them, your explanation helped a lot, thanks. – Abhishek May 24 '16 at 09:03
  • 1
    Tricky, but works great! Thank you! Android-Devs are really crazy : ( – Vladyslav Savchenko Jan 02 '17 at 19:03