4

I am writing an Android AppWidget. The layout for the widget contains a LinearLayout and, inside that, a number of ImageViews. In the AppWidgetProvider OnUpdate method, I first create a RemoteViews for the layout ...

var rv = new RemoteViews(context.PackageName, Resource.Layout.WidgetLayout);

... and, using that, I can set the bitmaps for each of the child ImageViews.

However, what I really want to do is to get the actual size of the ImageViews once layout is complete so that I can draw exactly sized images (probably via Bitmaps) into them. I see no way to get the ImageView sizes via RemoteViews, nor any way to get to the underlying ImageView of a RemoteViews item.

Can I even do this? Is it done in OnUpdate()? If so, how? If not, where should I be doing it?

Thanks in advance.

Boulder Keith
  • 535
  • 1
  • 5
  • 10

2 Answers2

4

Can I even do this?

No. The rendering of the UI defined by the RemoteViews is handled by a separate app (home screen) in a separate process. You have no means of getting at Java objects from other processes, Views in particular.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
2

You can get size of widget - approximate size of widget. In layout, you can use "weights" - and if you know sizes of widget, and weights of images - you can approximately calculate sizes of images. I am using such approach to make proper bitmaps. One of problems - is different devices lie differently. One of my bitmaps has next weights - 0.6 of width, and 0.15 of height.

        SetTitleButton( context,  views, (int) (w*0.6),   (int)(h*0.15*coefficient));

where coefficient is attempt to compensate different "lie" and user can modify it to fit...

w and h - are size of widget.

Bundle awo = appWidgetManager.getAppWidgetOptions( appWidgetId);
    int h=awo.getInt("appWidgetMaxHeight");
    int w=awo.getInt("appWidgetMaxWidth");
  • 3
    To avoid hard-coded string, I suggest using `AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT` and `AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH` instead. – Samuel T. Chou Dec 02 '20 at 06:35