Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get app widget size?

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.

like image 570
Ran Avatar asked Aug 08 '11 15:08

Ran


People also ask

How do I find the size of a widget?

To get the size/position of a widget on screen, you can use GlobalKey to get its BuildContext to then find the RenderBox of that specific widget, which will contain its global position and rendered size.

What are application widgets in Android?

App widgets are miniature application views that can be embedded in other applications (such as the home screen) and receive periodic updates. These views are referred to as widgets in the user interface, and you can publish one with an app widget provider (or widget provider).


1 Answers

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);
  }
}
like image 133
Budius Avatar answered Sep 21 '22 19:09

Budius