Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting resizeable appwidget dimensions (or getting around it)

I created a nice widet for my app. It just displays a list of items downloaded from a server. Well, to be precise, it only looks like a list, since ListView in RemoteViews is not supported in older android versions (which I want to support).

I want my widget to show a list of up-to-date items and I want these items to take all available widget space. To better show my problem, lest's assume WLOG that I want a widget with the TV schedule of selected programme. Knowing that:

  • Downloading from server is costly
  • WLOG we can assume that there are Aleph zero items on server that I can download
  • The TV schedule may change, so we don't want to download schedule for a whole month, but at most half of a day
  • The first item displayed will contain show currently being broadcasted

How I can ensure that there will be no empty space at the bottom of the widget if the user will resize it to make it larger? I searched SO and it seems that when user will change widget size (on ICS or custom launcher - GO Launcher EX allows it) it will be impossible (Correct me if I'm wrong) to get widget's new dimensions.

But I don't really need them! I would like to know how many items will fit in the widget and calculating this from widget's size is just one of the methods.

So, I'm asking you if it's possible to, for example, receive notification if a remote view will become visible, or create custom view for use in RemoteViews which would somehow allow me to estimate the number of visible items.

If it won't be possible, I will just have to use some reasonable defaults, but it would be nice to know how many rows I can show.

like image 662
user1234567 Avatar asked Feb 08 '12 12:02

user1234567


1 Answers

Since Android 4.1 (API level 16) you can detect size changes with onAppWidgetOptionsChanged. Take a look at this answer for a simple example. With this callback at least you can track widget's size ranges (pity you cannot track actual widget sizes). With prior versions and custom launchers you're out of luck: AFAIK there's no generalized way to get actual widget size ranges, even less so an actual size.

What I would do in your case is to fill the widget with a number of views that fit in the default widget size, as defined in the appwidget provider. That way you avoid getting blank space at the bottom, unless there are actually few items to show, of course. When the widget gets resized, you can show more views using onAppWidgetOptionsChanged with API level 16 +, and with API level < 16 AFAIK you will get empty space, but in that case you could trigger a widget update soon to minimize this issue.

like image 119
Jose_GD Avatar answered Sep 18 '22 05:09

Jose_GD