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.
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);
}
}