I need to make a 4x1 cell widget. For this, I use simple code:

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="250dp" android:minHeight="40dip" android:updatePeriodMillis="0" android:initialLayout="@layout/widget" android:configure="ru.bartwell.myapp.WidgetActivity" /> 

I am testing it on two Genymotion virtual devices:

  1. Google Nexus 7, 800x1280, Android
  2. 4.4.4 Samsung Galaxy S5, 1080x1920, Android 4.4.4

And I get two different results:

With android: minWidth = "250dp" and android: minHeight = "40dip" (new formula):

  1. Google Nexus 7 - 3x1
  2. Samsung Galaxy S5 - 4x1

With android: minWidth = "292dp" and android: minHeight = "70dip" (old formula):

  1. Google Nexus 7 - 4x1
  2. Samsung Galaxy S5 - 4x2

As you can see, the size of the widget depends on the screen resolution and in any case is calculated incorrectly (either 3x1 or 4x2). So how to set the size of the widget?

1 answer 1

I understand that this is not a matter of resolution, but of the layout of the device.

Indeed, you took a good example to check the widget's default-size when creating it, since one device like phone and the markup most likely has 4x4, and another type tablet , there is another markup, maybe 6x6.

Google guide offers the formula: 70xN-30

where N is the number of columns or rows.

For example, in order to make the widget 4 cells in length: 70x4-30 = 250dp - install android:minWidth="250dp" - it really works on phones, but as soon as we try to put on the tablet, a problem arises.

I can offer a solution from my little experience:

  1. android:minWidth="1" android:minHeight="1" set these parameters, and in the same place write android:resizeMode="horizontal|vertical"
    Then you get a widget with 1 cell and can stretch to the desired size, such an example has its drawbacks but it works.
  2. You can divide the installation of the widget, for phone or tablet.
    For a 4x4 phone, we use the formula 70xN-30 , for the tablet I used the value 96x96dp, this is the full cell size.

That is, in your example, for Google Nexus 7 , to make a widget with an initial size of 4x2, you need to set the values:

 android:minHeight="192dp" android:minWidth="384dp" 

It all depends on the end result, but I hope this will help you at least a little.