16

It might be me, but when I calculate minimum app widget sizes according to the formula given on the android page I don't get the right widget widths; The formula is as follows:

width(n) = (70 x n) - 30

When I want to have a 5x1 widget, the correct width would be (5 * 70) - 30 = 320dp. However when testing this on a motorola Xoom it resolves to being a 4x1 widget. I've tested different values and 400dp seems good for 5x1 on the motorola xoom with Honeycomb, but then I'd test it on a regular Galaxy Tab with Gingerbread and then it resolves to a 6x1 (like one would expect).

So two questions here;

  • What difference between Gingerbread and Honeycomb am I overlooking?
  • Since I know ICS widget size no longer has padding between widgets, is there some rule of thumb here as well?
MrJre
  • 7,082
  • 7
  • 53
  • 66

4 Answers4

24

In my 4x1 widget, I used these dimensions for res/values/dimens.xml:

<!-- size = (74 x n) - 2 -->
<dimen name="appwidget_margin">0dp</dimen>
<dimen name="appwidget_min_width">294dp</dimen>
<dimen name="appwidget_min_height">72dp</dimen>

and for res/values-v14/dimens.xml:

<!-- size = (70 x n) - 30 -->
<dimen name="appwidget_margin">0dp</dimen>
<dimen name="appwidget_min_width">250dp</dimen>
<dimen name="appwidget_min_height">40dp</dimen>

I used widget templates pack for background images .

In official guide there is written that default margin in res/values/dimens.xml should be 8dp, but if I use 8dp, my widget is smaller than standard widgets on the desktop (google search, weather etc.). Thats's because margin for v1-v13 is built in the background image:

enter image description here

I tested it on HTC Desire, Nexus S, emulator Android 2.2 and emulator Android 2.3.3. With the templates pack backgrounds and configuration above, my widget's size is the same as other standard widgets and looks good on all devices I tested.

There is also problem with various launchers. I recommend this article to read: http://radleymarx.com/blog/app-widget-padding-margins-in-ics-android/

petrnohejl
  • 7,581
  • 3
  • 51
  • 63
  • 1
    BTW, the author of the blog post you recommend is the same guy who wrote the accepted answer here ;) – Jose_GD Jun 26 '13 at 19:08
10

Not too many devices fully follow Google's advised formula. You're better off using several xml-xxx folders that can specify more accurate minWidth & minHeight that correspond to the varying screens & OSes.

Right now I four solely based on OS:

xml       // standard
xml-v11   // Honeycomb grid
xml-v14    // ICS's new extra padding
xml-sw552dp-v14  // ICS tablet padding

But as I fine tune, I may have to add a few new folders for particular dpis or screens.

Quick update:

Only some devices use automatic padding. Samsung & HTC have custom UIs that use full-width widgets, so they override the OS padding on their launchers.

radley
  • 3,212
  • 1
  • 22
  • 18
  • 4
    You are pretty much right to use 3 different OS sizes; widthICS(n) = (70 x n) - 30 is ICS only, width(n) = (74 x n) - 2 is for Gingerbread and earlier. I added 2 * 8 to the Gingerbread formula to apparently get the right Honeycomb size, which is not really accurate I would guess, but works. – MrJre Apr 24 '12 at 14:53
  • @MrJre I have zero problems using the Gingerbread and earlier formula on Honeycomb in my home screen widget. Tried with emulator (yes, I have a lot of patience) and with Galaxy Tab 10.1 – Jose_GD Sep 18 '13 at 20:17
  • 1
    @radley: "Not too many devices fully follow Google's advised formula". Well, my Nexus 7 with stock Android reports a 3x3 widget for my 4x3. I'm using minWidth=250dp and minHeight=180dp, as suggested in the docs. If Google sponsored devices behave like this, what can we expect from others... – Jose_GD Sep 18 '13 at 20:20
  • @Jose_GD there's a sweet spot that works for everyone. don't recall the formula though... – radley Sep 20 '13 at 21:50
  • @radley do you mean some "magic" value for minWidth and minHeight? For now, I obtained a 4x3 widget using minWidth=275dp for sw600dp devices. But don't know how this change will affect *every* device out there – Jose_GD Sep 22 '13 at 02:44
  • 1
    I have different minWidth / minHeight values for the three systems. We tested on a lot of devices got find this sweet spot. For a 4-grid wide widget: basic xml android:minWidth="288dp", xml-v11 android:minWidth="318dp", and xml-v14 android:minWidth="250dp" works for all devices. – radley Sep 23 '13 at 19:05
  • @radley thanks, but have you tested your 250dp width on a Nexus 7? I own one, and with minWidth=250 I get 3 cells, not 4 – Jose_GD Oct 10 '13 at 21:30
  • @Jose_GD ah, thanks for pointing that out. I double checked and turns out I had another folder for ICS tablets. The min width there is 318dp. I updated my info. – radley Oct 10 '13 at 22:03
  • 318dp didn't work for me (on a Nexus 9, Android 6.0), had to use 320dp!? For a 5x grid, I used 433dp after a lot of experimenting. 5x grid never worked on a LG G4 though, likely limited by the OS. – 3c71 Aug 27 '15 at 09:21
4

In ICS there isn't no padding, there is automatic padding. And the formula that you used is for ICS. For older versions there is another formula:

num*74 - 2
Yury
  • 20,618
  • 7
  • 58
  • 86
  • 1
    So 368dp should suffice for 5x1 widget width on a motorola xoom with honeycomb according to your formula, except it doesn't since I'm still getting a 4x1 widget with that width :/ – MrJre Apr 04 '12 at 12:48
  • Only some devices use automatic padding. Samsung & HTC have custom UIs that use full-width widgets, so they override the OS padding on their launchers. – radley Dec 05 '12 at 22:25
1

note: if you target pre-Honeycomb (or don't specify targetSdkVersion while specifying a minSdkVersion prior to honeycomb) then the honeycomb grid (and ICS grid) calculations don't take effect. Downside of this is you miss out on newer OS features, but if you don't actually need them then keeping target pre-honeycomb will save the hassle of customised xml folders.

straya
  • 5,002
  • 1
  • 28
  • 35