Is there any method to know auto resizing widget size in pixels before placing this widget on lockscreen? For example when we place a widget on lockscreen then configuration activity started and how this activity can get size of widget?
AFAICT, an app can't determine a widget's size until the user places the widget on the (lock or home) screen. Even then, the OS returns a size range [why?] and of course the user can resize the widget.
E.g. add this debug code to an AppWidgetProvider's onUpdate()
method:
Bundle newOptions = appWidgetManager.getAppWidgetOptions(appWidgetIds[0]);
int minWidth = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
int minHeight = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);
int maxWidth = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH);
int maxHeight = newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT);
Log.i("getAppWidgetOptions", String.format(
"getAppWidgetOptions %d x %d .. %d x %d",
minWidth, minHeight, maxWidth, maxHeight));
Running on a Nexus 4 emulator, a newly placed home screen widget reports: 240 x 58 .. 302 x 84
in dp units. This is with <appwidget-provider>
XML that sets:
android:minWidth="180dp"
android:minHeight="40dp"
android:minResizeWidth="180dp"
android:minResizeHeight="40dp"
An AppWidgetProvider can detect when its widgets resize by overriding onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions)
. With similar code as above [but omit the getAppWidgetOptions()
call], it logs ranges like 240 x 58 .. 302 x 84
and (after manually stretching the widget wider) 328 x 58 .. 408 x 84
.
Even on the lock screen, a widget can take screen space from the lock keypad if the user drags down over it, although I'm not sure if that changes these min/max size values.