https://kotlinlang.org logo
#compose
Title
# compose
d

Davide Giuseppe Farella

10/06/2020, 4:31 PM
Hey guys, given a
Copy code
Column {
  Text(...)
  Image(...)
}
which is the best way to let the the Text.width to be at most as Image.width? I don’t want the Text to exceed the Image size
z

Zach Klippenstein (he/him) [MOD]

10/06/2020, 4:48 PM
Probably with intrinsics? There's a section in the docs about this, I can dig it up if you haven't seen it
🙏 1
d

Davide Giuseppe Farella

10/06/2020, 4:52 PM
Can you share me the link pls? I can dig myself 🙂 Thank you 🙂
m

mattinger

10/06/2020, 5:16 PM
You could also use a ConstrantLayout as well, attach the image to the left and right of the parent, and attach the text to the left and right of the image. this should force the text to match the image width. It’ might be overly complex but it should work.
I’m not sure how the text’s intrinsic width would interact with the image’s intrinsic width, but this sounds like something they’d designed for
d

Davide Giuseppe Farella

10/06/2020, 5:53 PM
Thank you, I was hoping in an easier solution 😋 I'll take a look at both of the solutions, thanks
z

Zach Klippenstein (he/him) [MOD]

10/06/2020, 6:30 PM
A third solution is to just make a custom
Layout
that knows it only has two children, and what those children are, and can explicitly size itself appropriately. Making custom layouts is pretty easy with compose, so that might actually be easier/more readable/maintainable than using intrinsics depending how confusing they are.
🙏 1
2 Views