I've programmed Android for a while, but this really puzzles me.
So I have a simple appwidget with a textview. I simply want the text font size inside that textview to scale up/down if the user resizes the widget so as to make the text larger if the app is resized. I have tried several solutions but everything fails:
Simply using the "sp" (scaled pixels) unit for the font size - this does not scale the text when widget size changes (I did not expect it too as well!).
Extending TextView with my own and the overriding onSizeChanged method. But you cannot have descendents of basic UI classes in a widget (documentation also states this).
Since there are no methods to even get the current appWidget size I simply am stuck here.
Yes, I know one solution is to create different versions of the widget like 1x4 and 2x4 etc, but surely there is another way?
Any ideas are welcome?
It's not possible to automatically scale the text of a TextView based on parent size. A workaround would be to override the onDraw
method of the TextView and implement the scaling manually.
As for the appwidgets: You can't retrieve a resizable appwidget's current size because they are intended for use with ListView
and GridView
containers. These containers will manage the scaling and spacing of content automatically and will make themselves scrollable if the content can't fit in the appwidget's current size.
If you're not using a ListView
or a GridView
it seems to me the best way to "scale" the components of a appwidget is the method you already mentioned: Use different non-resizeable appwidgets.
You may call AppWidgetManager.getAppWidgetOptions(OPTION_APPWIDGET_MAX_WIDTH) to get the max width (same way to widget's height) and adjust text size accordingly. Limitation: this method is available from API Level 16
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With