9

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.

user1234567
  • 1,832
  • 1
  • 18
  • 25
  • I like the question. What can be done is when you update a widget and do the remoteViews.addView(...) you can set a counter perhaps on how many new views have been added to the remoteView? – Sergey Benner Feb 08 '12 at 12:15
  • @SergeyBenner How will it help in my case? I can add as many views as I want, addView() won't fail or notify my if the view I'm just trying to add won't fit in it's container. – user1234567 Feb 08 '12 at 12:27
  • okay. You are using AppWidgetManager to update your widget http://developer.android.com/reference/android/appwidget/AppWidgetManager.html#ACTION_APPWIDGET_UPDATE system gets this broadcast on widget resize or anything. you can use getAppWidgetInfo() to get to http://developer.android.com/reference/android/appwidget/AppWidgetProviderInfo.html and I think it has something what you need. – Sergey Benner Feb 08 '12 at 12:41
  • I am also receiving the following action com.motorola.blur.home.ACTION_SET_WIDGET_SIZE when i resize my widget and you can also check this link (http://www.droidxforums.com/forum/droid-x-hacks/21620-creating-resizable-widgets.html) but as you have guessed it won't work on all launchers of course (using LancherPro here on dell streak5) – Sergey Benner Feb 08 '12 at 12:57
  • @SergeyBenner Thanks! com.motorola.blur.home.ACTION_SET_WIDGET_SIZE seems to be de-facto standard across custom launchers. I still didn't figure out how to support the default ICS launcher though. It doesn't seem to send me any broadcast when widget is resized. – user1234567 Feb 08 '12 at 13:36

1 Answers1

4

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.

Community
  • 1
  • 1
Jose_GD
  • 2,279
  • 1
  • 21
  • 34