Android home screen widget textsize dynamically - android

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.

Related

Reliable way of getting initial size of QWidget

I have a custom widget. It's purpose is to display an image (with possibility of scaling, rotation etc).
My goal is to calculate initial scale of the image so its size would match widget's size.
My first attempt was to do it in constructor, but it wasn't right place (as widgets are usually put into layouts after construction, so their size change later).
Another way, which partially works is to override showEvent or resizeEvent and put there initial scaling. It works partially, because it works fine on desktop but for some reason it doesn't on android device - QWidget::size() returns the same size as in constructor.
Apparently on android first show/resize events are called before final window rearrangement.
Is there a clean way to achieve it?
You can try using a QLabel, either instead-of or inside the widget (you can set its border to 0px) and use setScaledContents.

How to get Home Screen Widget Size

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.

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.

Autosizing text in TextView on Android

Is it possible to set TextView's text size to be the same as TextView height, when TextView height isn't predefined(WRAP_CONTENT or FILL_PARENT)?
solution : Auto Scale TextView Text to Fit within Bounds
i also wanted to do something like this and the closest you can seem to get is to say android:textSize=20dp (or whatever size you think is appropriate) for either your style or each element that is displaying text. since dp is device independent pixels, if it appears to be taking up the whole screen on your device, then it is supposed to appear that way on all other devices too. you might want to check on this as you might have to choose a different dp value for each of the different size/density combinations possible (depending on what kind of devices you are aimed at, also whether you are allowing the use to change the orientation) this has all that info.
I've found the library that do exactly what I want : SizeAdjustingTextView

Categories

Resources