Calculate text layout with StaticLayout without allow word-wrapping - android

Is there some way to use StaticLayout without doing automatic word wrapping? If the text includes newlines I want them to break up the paragraphs, but I don't want it wrap at a given width. It's not giving sane results, especially with Center/Right aligned text.
Is there a way to simply turn off the auto-wrapping on StaticLayout?
More details. I use getDesiredWidth to calculate a width and then use this as the wrap width for a new StaticLayout. This prevents auto-wrapping, but it also seems to turn off explicit wrapping as. GetLineCount returns 1 for text that actually has multiple-lines.

Related

Dynamic ellipsis support for Android autosizing TextViews

The new Autosizing TextViews are pretty awesome, but it seems a fundamental thing is missing: ellipses.
Adding ellipses still requires defining the maxLines attribute, but if I want to be able to dynamically resize the text size according to the text view boundaries, I'd also like to be able to dynamically add ellipses when needed. Right now, if the text doesn't fit even with the minimum text size, it just gets cropped.
How could I add support for dynamic ellipses without giving up the new autosizing support?
The best solution I came up with so far was to programmatically set the maxLines to the proper value on runtime. Something like this will get the job done:
fun TextView.setMaxLinesForEllipsizing() = doOnPreDraw {
val numberOfCompletelyVisibleLines = (measuredHeight - paddingTop - paddingBottom) / lineHeight
maxLines = numberOfCompletelyVisibleLines
}
Be aware that this depends on Android KTX (but can also be easily achieved with a regular OnPreDrawListener).
Then we can simply call this extension from any TextView we want to get the dynamic ellipsis.
textView.setMaxLinesForEllipsizing()
If the text changes it might be necessary to call it again, though. So it might also possible to reach a more complete (and complicated) solution by moving this logic to a custom TextView and maybe overriding onTextChanged() there.

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

Dynamically Sizing the height of ListView components

for practice I am writing an activity that takes a string from a local DB and puts it into a listView. However the string's are of different length, ranging from a couple of characters, to a couple of sentences. Write now I can take them and place them in a listView but, not all of the strings fit. I was curious if/how you can dynamically choose the height of each part of the list view so that the entire string can fit in it.
Just make sure your list item layout is using wrap_content for its layout_height attribute. If you're using a fixed height, it won't scale to fit larger strings. If you want the lists a specific size except for in situations as mentioned, you can also add a android:minHeight attribute as well with the dimension you want as standard.
Also, make sure your TextView is set to multiline (attribute android:singleLine="false").

Is there a fill parent for Text Size?

I am trying to make a sign (the ones that drivers use at the airport to find someone "Mr Smith") and I wanted the sign to have the largest Font size possible, (its for a tablet), I could write a function to change the size depending on the length of the Text but is there a way of doing natively/better?
Thanks
The most flexible way is to create a custom view that draws the text in onDraw(). When drawing the text (with one of Canvas.drawText()) you will have to provide a Paint, and that would let you know the precise length of text, not just length of string (see Paint.measureText()).
This way you would have plenty of ways to calculate and redistribute the space (and it would totally rely on you how).
One way I can think of is calculating the length for a huge font size and then using the h/w ratio to see if I want to fill the screen in width or height.

Calculating Android TextView's text capacity

I have TextView with height and width as fill parent. Is it possible to find out how many characters can this layout hold?
Do you mean how many characters can be entered into the textview and still be fully visible without scrolling? For proportional fonts, that will depend on the specific characters typed, including where the line break opportunities are. I don't think there's a simple way to compute that.

Categories

Resources