Reputation: 5258
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 RemoteView
s 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?
Upvotes: 2
Views: 3132
Reputation: 46498
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+.
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)
TextView
's width from the widget sizematch_parent
to the root of the widget, mind margins/paddings)widget = LayoutInflater.from(context).inflate(R.layout.widget, null)
widget.measure(MeasureSpec.makeMeasureSpec(widgetWidth, MeasureSpec.EXACTLY), ...)
.TextView
's size: widget.findViewById(R.id.myText).getMeasuredWidth()
refitText
here to find your optimal sizeNote: 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.
Upvotes: 2