Change textsize in Android button if text is too long - android

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

Related

Is it possible for content (size) to match parent?

If I have a text in a button that has match_parent, is it possible to make the textSize as big as possible without cropping the text? Preferably in XML and with a maximum setting so it doesn't become too big. In other words I'd like it to make it just fit if it would split a long word, otherwise stick to the preset size.
This way, but it does not look clearly in my opinion you should to try to change your design to avoid this requirement

Android: String fill text box

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

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

Android - TextView on an ImageView, with bounding size equal to the Imageview Size

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.

font diaplay half when TextView is too short

the textView's height and width is fixed.
when the length of string is longer than textview,the last-line text sometimes display half.
I want,when the last-line text can't display complete,discard it.how to realize?
It sounds to me like you already have ellipsize enabled, and that's why you're only getting "half" the last line.
Depending on what you're trying to achieve, you can-
Remove ellipsize settings and suffer
the string cut-off.
Resize your textview so that all the
text will fit.
Change your font size so that all the
text will fit.
StaticLayout has a way of detecting if the last line will cut off (using getEllipsisCount(line)) which is supposedly used internally by TextView, but I've had no luck using that to detect when a TextView is being cut off (see post).
If this doesn't help, please give us a better picture of what you're doing.

Categories

Resources