I don't really know where to look as I'm starting out with Android.
How would I automatically make a String assigned to a Button in Android, take on an appropriate font size according to the button's size?
extend class Button, override onMeasure method and there you can compute view's height and set proper text size
Related
I have buttons with fixed size, but the text is changed time to time.
Sometimes the text is too long to fit in the button, and for these cases I want to use a smaller textsize.
How can I change the button textsize if text is too long?
(One solution could be to test how many characters that can be used with the normal textsize, and then change textsize if the length is larger than this baseline. But I was hoping for more dynamic approach.)
1)Measure the button.
2)Using the same font, use Paint.getTextBounds() to get the width.
3)Compare the size of the button to the width. You'll probably need to add some extra room on both sides for padding, but this is going to be a bit of an estimate anyway.
4)If the text was too big, reduce the size of the text (on the Paint object) and goto 2.
5)Now that you have a working size, call setTextSize on the button.
Note: if you're doing this for an AlertDialog, you need to do it after the button exists- I've had problems with step 1 depending on where I put this function, but its been so long I forgot the exact issue. I think I had to do it after calling show?
You can Extend Button Class to something similar to AutofitTextView
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.
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.
I am building an app and based on the content and screen size I want to dynamically set the font size to fill the entire screen.
Is there a way to determine the width of a line of text (in pixels)? I want to avoid the flicker that trial and error can present the user. Can it be done without first displaying a TextView?
The Paint class has a measureText method for this purpose :)
this one's be puzzling me for a bit... hopefully there's an easy solution.
I want to create a textView with a background (think in the context of a form of some sort). The problem is, I want the maximum size of the textview to be bound to the size of the background image. I've tried a couple of ways:
- Using the background property of TextView directly, but this scales the image when the text gets big
- Using an ImageView and TextView overlayed, but I couldnt work out how to get the textView width to bind to the width of the ImageView
- Hardcoding the maxWidth property of the textView to some dp value. This works - although, I seem to come across variants of this problem all the time and haven't figured a way to do it dynamically.
Any ideas??
Cheers!
You might want to create a custom view that derives from TextView, and then override the setBackground method and where you can force the size of your text view to the size of background.
One more word, you might also use android:ellipsize to marquee to avoid overflowing of text, this is a common technique used in the API.