Is it possible to measure `Composable` without ren...
# compose
k
Is it possible to measure
Composable
without rendering it? Use case: I’m writing a
Table
component (
LazyColumn
of `Row`s), and upfront I know which cell in a column will be the widest one (the one with the longest text), so I would like to measure such cell before rendering and use its width for cell in a column. Without that, columns resize to adjust to the widest one while scrolling and discovering more rows. I would like to avoid this effect
s
Don’t know how you’d do that exactly, but I do think there is a super hacky way which would probably work. Since you say you know for a fact how big the biggest one will be, and you got that specific string on your hand at all times. You could have every single item on your lazy list contain an extra text composable, which will have this same string, and you can put it in the layout, in a box with whatever else you’re putting there so that they are on top of each other, but simply have it not show at all, either by having 0 alpha, or doing this https://kotlinlang.slack.com/archives/CJLTWPH7S/p1672870701280349?thread_ts=1672839190.220699&cid=CJLTWPH7S Until you get a better answer I’d try this personally. And it’s just 1 more Text composable, should hopefully not be the end of the world performance-wise.
e
in general, longest text isn't necessarily widest text (variable font width. even with fixed width fonts, wide characters may be double width, combining sequences may be zero width, and emoji are their own ball of fun)
s
Yeah very true, hence I said maybe do this which is very straightforward and dumb, until someone comes up with a better solution 😅 which I have no idea how it might even look like
e
it's possible in a non-lazy Column
I don't see a general solution for lazy without composing everything, which defeats the point of lazy…
d
You could use TextMeasurer to measure all the texts and find the widest one. It won't give you the full measurements of your actual composable of course so you'd need to consider the non-text elements too somehow before you know your actual widest element.
k
Thank you for all the suggestions. TextMeasurer looks very interesting especially because the rest of the width is the padding that I also know upfront, so measuring whole component should be possible.
s
Does text measurement account for what happens if it breaks into the next line if the text is long? Which I guess is another thing to worry. Unless you don't let it go to the second line at all
k
It's set to be one line, so I don't need to worry about it, but thanks for heads up
e
embedded newlines may force it to multiple lines. textmeasurer does handle that
d
It also supports checking for overflow which is pretty cool
j
Probably want to check LazyVerticalGrid or do custom Layout or SubComposeLayout to do measurements wanted.
z
This is probably a good use for intrinsics.