zaifrun zaifrun - 10 days ago 4
Android Question

How to resize text size depending on widget size in a textview in an appwidget?

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:

  1. 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!).

  2. 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.