3

I can't seem to find anything on the following problem with a widget I want to code: Apart from the resize-ability introduced in Android 3.1, I want my widget to be resizable in custom launchers like LauncherPro or ADWLauncher. Is there a way I can define how my widget changes its layout once it's resized? So, for example, button A is shown on the left for 2x1 but is shown on te right for 3x3. Also I want it to change its appearance/layout when due to screen rotation the widget size changes (because e.g. 3x1 has different height and width in landscape and portrait).

Any hints would be helpful. Thanks a lot! Till

Till - Appviewer.io
  • 4,529
  • 1
  • 31
  • 35

2 Answers2

8

As of Android 4.1 it is possible to use onAppWidgetOptionsChanged() to find out the size of a widget on resize and change the layout. So in your AppWidgetProvider:

@Override
public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle bundle) {
        int minWidth = bundle.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
        int maxWidth = bundle.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH);
        int minHeight = bundle.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
        int maxHeight = bundle.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT);
        RemoteViews rv = null;
        //Size of a 2x1 widget on a Nexus 4 (other screen densities will differ)
        if(minWidth == 152 && maxWidth == 196 && minHeight == 58 && maxHeight == 84){
            rv = new RemoteViews(context.getPackageName(), R.layout.widget2x1);
        } else {
            rv = new RemoteViews(context.getPackageName(), R.layout.widget);
        }
        ...
        /* Set some stuff in your layout here */
        ...
        appWidgetManager.updateAppWidget(appWidgetId, rv);
}

The trouble is you need to find the min/max width and height for each screen density/size in order to know when the widget is resized a certain amount to warrant a layout change.

Mark
  • 3,334
  • 1
  • 22
  • 27
  • 1
    Thanks for the answer but note that OPTION_APPWIDGET_* values are reported in dp unit. That is, there is no density trouble you mentioned. – Zsolt Safrany Aug 01 '13 at 17:03
  • 1
    You should rather define intervals instead of using the == operator. This will make your layout selection more robust among different home screens. – Zsolt Safrany Aug 01 '13 at 17:05
0

Is there a way I can define how my widget changes its layout once it's resized?

Your app widget doesn't change its layout once it's resized.

So, for example, button A is shown on the left for 2x1 but is shown on te right for 3x3.

That is not the purpose of resizeable app widgets. Resizing is for the use of ListView, GridView, etc., that will absorb the additional scrollable space.

Also I want it to change its appearance/layout when due to screen rotation the widget size changes (because e.g. 3x1 has different height and width in landscape and portrait).

For that, you should be able to use standard orientation resource sets (e.g., res/layout/ and res/layout-land/).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Thanks for the quick reply. So the only way to have different (-ly structured) elements in a widget depending on its' size is to choose the desired layout depending on the user's choice of size at the time of widget install (e.g. winamp widget incl. album art for 4x2, not for 4x1). And to allow proper resizing on-the-fly in the 3rd party launchers, the only thing I could consider is avoiding fixed width and height values so that components can actually absorb additional space? – Till - Appviewer.io Jun 24 '11 at 01:36
  • @Till: That's pretty much spot on. – CommonsWare Jun 24 '11 at 10:20
  • You may not be "supposed" to change layouts while resizing, but I found at least one example where this is done: Samsung's "Accuweather.com" widget which is presintalled on the Galaxy S II, Galaxy Tab 7..0 Plus, Galaxy Note, among others I'm sure. When you resize the widget it will change it's layout. – christoff Apr 25 '12 at 00:05
  • 1
    @christoff: Unless it exhibits that same behavior on a third-party home screen, what you are seeing is probably something proprietary between that pseudo-app widget and Samsung's home screen. Implementers of home screens can create "app widgets" that are not really app widgets, but rather are features of the home screen app itself, and can do all sorts of extra things. – CommonsWare Apr 25 '12 at 10:53
  • @CommonsWare That make sense because on further inspection Samsung enabled resizing on < 3.0 devices which has to be built into the launcher. – christoff Apr 26 '12 at 19:12
  • That is strange because the design guide explicitly mentions that "You will have to dynamically adjust your widget's content and layout to the size the user defined through the resize operation." (http://developer.android.com/design/patterns/widgets.html) – Zsolt Safrany Aug 01 '13 at 17:00
  • @ZsoltSafrany: This answer is from two years ago, and resizing of app widgets has changed since then. – CommonsWare Aug 01 '13 at 17:19