How to get Home Screen Widget Size - android

In android 4.2, there is a new callback called onWidgetOptionChanged() in which a bundle containing the widget size information is passed back.
http://developer.android.com/guide/topics/appwidgets/index.html#AppWidgetProvider
My question is, how do we get the widget size prior to android 4.2?
I need to set a picture to an ImageView in the widget but I need to know the size of the ImageView / Widget to scale the picture accordingly.
Also, the layout used for the widget changes depending on its size. Without onWidgetOptionChanged(), how does one know when to use a different layout depending on the size of the widget?

I'm afraid that you cannot get current widget size on android before 4.2. Only preconfigured widget size through android.appwidget.AppWidgetManager#getAppWidgetInfo.

Related

can flutter adjust widget size automatically in different device?

I'm new in flutter and haven't used android before. When I complete pages, I simply set the width and height of a widget according to the XD designed by UI designers. As I know UI designers usually set the size according to some one phone type. My question is: If the flutter program run in different devices, how can I change widgets' size automatically to adapt to the phone(or ipad). As we can imagine, if my program is based on ipad mini(1024x768), the designed widget size will be a little big, then some error maybe occur if I run the program in a samller device(like modbile phone) I think. Thanks for any help.
If all widget's are placed static and have proportional size, then you can use https://pub.dev/packages/flutter_screenutil, this library or MediaQuery widget.
Or design have ratio size, then you can use Flexible and Expandable widgets as you need.

Android home screen widget textsize dynamically

There are applications that offer the ability to change the font size of a text in a home screen widget. One example is https://play.google.com/store/apps/details?id=org.zooper.zwfree
However home screen widgets only can carry RemoteViews so setting the textSize of a TextView dynamically will not work.
As I see it there are two possibilities to change the text size dynamically:
Add for every text size another layout.xml file. Those files merely differ in the TextView's textSize value. When the user wants to change the textsize, the respective layout has to be loaded.
Draw a Bitmap instead of creating a View like here https://stackoverflow.com/a/4411060/883083
My question is: Is there a third possibility left?
If you're targeting API level 16 or above, you can try the following:
Sadly the whole thing depends on knowing the widget size, which is only possible in API 16+.
Override the AppWidgetProvider.onAppWidgetOptionsChanged callback
or get the same Bundle later via AppWidgetManager.getAppWidgetOptions
Extract the size of the widget:
options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH)
options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH)
options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT)
options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT)
Deduce your TextView's width from the widget size
(best if you have it match_parent to the root of the widget, mind margins/paddings)
If you have complex layout you can alternatively
inflate the whole widget in your app space
widget = LayoutInflater.from(context).inflate(R.layout.widget, null)
Simulate a layout based on the framework:
widget.measure(MeasureSpec.makeMeasureSpec(widgetWidth, MeasureSpec.EXACTLY), ...).
Get your TextView's size: widget.findViewById(R.id.myText).getMeasuredWidth()
Use something like refitText here to find your optimal size
Set the calculated size via RemoteViews.setTextViewTextSize
Note: I didn't implement this method, just thought about it.
Try not to do this on every update, cache the results (even in preferences), widget options shouldn't change often.

Scaling Android Widget

I have a widget with 2 oval buttons (defined in a XML file). At the moment, I've hard coded the width and height of the button to 180dp, but this poses problems when making the widget smaller.
Is there anyway I can set the width and height of the button to scale as the widget size is changed?
Resizable widgets are preferred feature for collection widgets
such as those based on ListView or GridView. App Widget Design Guideline
So for your purpose it's better to use different layout if your widget gets resized. On Android 4.1 and above you may use onAppWidgetOptionsChanged() method to load different layout when your widget is resized. See these answers for more information:
How to use onAppWidgetOptionsChanged() in a widget?
define widget's behaviour/layout for resize and orientation change

Getting a widget's width

I'm trying to use a custom font with the text in my Widget. Since the RemoveViews is very limited and doesn't support custom fonts in the TextView options, I found this post that showed a clever way of getting around this. Essentially, instead of using a TextView I use an ImageView and render a bitmap of the text in the custom font and set the ImageView's bitmap to the rendered image. Now the only problem is that I need to determine the width to render the Bitmap so it fits perfect in the fill_parent on the ImageView. So how do I get the width of a widget?
I'm trying to use a custom font with the text in my Widget.
Note that the correct term is "app widgets". Widgets are subclasses of View. App widgets are the things that go on the home screen. See: http://developer.android.com/guide/topics/appwidgets/index.html
So how do I get the width of a widget?
You don't. You know what you asked for via your metadata. What you wind up with is up to the home screen implementation, and there is no way to retrieve that information. That goes double for widgets that the user resizes, either because you indicated that you support resizing, or the home screen just decided to roll that feature themselves.

How to compute app widget bitmap size?

I have a working widget that uses layouts and RemoteViews and scales itself nicely to whatever home screen area assigned to it. However, I need to display custom fonts and thus must re implement using an explicit bitmap and here is where the problems start, computing the bitmap size ended up to be a tough problem.
What is a good formula to compute the widget bitmap size in pixels as a function of these
values (and anything else that is available and is useful):
Number of home screen rows (R) and columns (C) allocated to the widget. (I derive min/max value in the widget_info.xml from these values).
Display metrics (screen size, DPI, density (D), etc)
Current orientation (O)
Android version (V)
The goal is not just to find a safe size but also not wasting screen real estate.
Just call getWidth and getHeight on the parent layout of your Widget. After the widget has been drawn once these values will be populated and you'll know the exact size of the widget.

Categories

Resources