7

I have an Android app widget and I would like to extend it to support several sizes and also re-sizable with Honeycomb.

The problem is that I need to know what is the size of the widget , so I can know how much content I can put in it.

How do I read the app widget size?

I couldn't find anything.

Ran
  • 4,117
  • 4
  • 44
  • 70

1 Answers1

16

I know it's old question, but there's a newer answer to it (that I believe was not available on the time, as it is API 16 and up only):

you can call:

Bundle options = appWidgetManager.getAppWidgetOptions(appWidgetId);

that will give the same options that is passed to your receiver during

public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions)

with this Bundle options you can query for the sizing using the constants:

AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT
AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH
AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT
AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH

Remembering that Samsung likes to be different, so you probably need a special Handler for TouchWiz (code bellow copied directly from my app):

@Override
public void onReceive(Context context, Intent intent) {
  if (intent == null || intent.getAction() == null)
     return;
  // I FUCKING HATE SAMSUNG!
  if (intent.getAction().contentEquals("com.sec.android.widgetapp.APPWIDGET_RESIZE") &&
     Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
     handleTouchWiz(context, intent);
  }
  super.onReceive(context, intent);
}

@TargetApi(16)
private void handleTouchWiz(Context context, Intent intent) {
  AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);

  int appWidgetId = intent.getIntExtra("widgetId", 0);
  int widgetSpanX = intent.getIntExtra("widgetspanx", 0);
  int widgetSpanY = intent.getIntExtra("widgetspany", 0);

  if (appWidgetId > 0 && widgetSpanX > 0 && widgetSpanY > 0) {
     Bundle newOptions = new Bundle();
     // We have to convert these numbers for future use
     newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, widgetSpanY * 74);
     newOptions.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, widgetSpanX * 74);

     onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);
  }
}
Budius
  • 39,391
  • 16
  • 102
  • 144
  • 1
    how did you come up with widgetSpan * 74? Just trial and error? Do you know majic number for max_height?? – Nathan Schwermann Jul 03 '14 at 17:23
  • sorry the `handleTouchWiz` was directly copied from some other SO answer. I have no idea, where it comes from. – Budius Jul 03 '14 at 23:54
  • Just tested on Galaxy S6, the intent is never received, but the onAppWidgetOptionsChanged is called with valid parameters, so Samsung seems to have fixed this. – joe1806772 Jan 13 '16 at 13:03
  • Trying this on a tablet, i just noticed it doesn't fire on orientation change. Plus even if you force it to fire with a reisze the values seems to be wrong ( the same as in portrait ) – Mario Lenci Feb 23 '16 at 11:19