I have a ```Row { Button("!Yes") Button("No",...
# compose
d
I have a
Copy code
Row {
  Button("!Yes")
  Button("No", Modifier.weight(1f))
}
And it causes line break to appear for some reason. If I replace
"!Yes"
with even longer
"HelloYes"
, than no line break occurs. Why is this happening? P.S. Using
".Yes"
results in no wrapping too, but using
"?Yes"
wraps. It's quite selective. UPD Turns out this is a custom
Button
component and although it's based on a material one, I can't reproduce this with pure-material buttons. Investigating...
😲 2
p
It will try to make the right button as big as possible. And also try to make the left one as small as possible. I guess text by default breaks per-word. I think if you change your text to "Hello Yes", it will also create a line break.
c
I can’t speak to much of the specifics, but the line-breaking algorithms are quite complex as they try to provide the best result when text is read naturally. I recall reading an interesting article years ago about how Amazon’s Kindle uses a different algorithm for calculating line breaks than the rest of the Android SDK, because the slower refresh rate of e-ink allows them more time to perform better optimization. Android’s default text algorithms favor speed over aesthetics, and sometimes get the result wrong as a result. Line breaks don’t happen in the middle of words (camelCase doesn’t get read as two words, but just one); breaks are put between words and whitespace and/or punctuation. The fact that
.Yes
does not break while
!Yes
and
?Yes
do is likely due to the fact that
.
has a shorter-width than
!
or
?
.
All that aside, what you probably are missing from the Material button is setting the
maxLines = 1
in the
Text()
composable, or including a
Modifier.defaultMinSize(width = ...)
d
Thanks for suggestions! It turns out that all this was caused by adding
Modifier.width(IntrinsicSize.Min)
to the
Button
call. I'm not yet sure why this was done (not my code), but if I remove this modifier, it measures "!Yes"-button correctly and no line breaks are inserted.
z
That makes sense, the minimum intrinsic width of a text is the width when every line-breaking opportunity is taken.
d
Oh, didn't think about this, interesting. Looked like a bug in compose, but now it doesn't 🙂