1

I have a lazy row with items that have wrap content height. my problem is that some texts of these items is visible or invisible base of that item. so the height of that cart is not fix and it will change.

how can I find the max height of that item (with all texts) and set that height to all items.

I do not want to set a hard code height to my items (like 300.dp)

as you can see in this image: the below button change its position based on the card's height. I want that button fix in its place and not move up and down. https://i.stack.imgur.com/R0Tc6.png

how can I fix that problem?

melikaafrakhteh
  • 305
  • 1
  • 2
  • 8

2 Answers2

2

Have you tried experimenting around onGloballyPositioned{..} modifier property?

val localDensity = LocalDensity.current
                
Text(modifier = Modifier
           .onGloballyPositioned { thisText ->
               with (localDensity) {
                   thisText.size.height.toDp()
               }
           },
           text = "Text"
)

Edit: Intrinsic Measurement is not allowed in LazyList components, have a look at this post, looks like similar to yours.

Jetpack Compose: Row with all items same height

Also, Constraintlayout maybe a good thing to experiment on.

z.g.y
  • 5,512
  • 4
  • 10
  • 36
  • Thanks for your answer, what I did: I found my first cart's height used onGloballypositioned() and store it in a variable. Then I compare the next card's height with that variable, If that height is bigger than the variable then the variable stores that, and my lazy row uses that variable as min height. – melikaafrakhteh Sep 27 '22 at 15:10
1

One of the rules of Compose is that you should only measure your children once; measuring children twice throws a runtime exception. However, there are times when you need some information about your children before measuring them.

If you have a look at the Android documentation for Intrinsic Measurements, you will have a clear idea of what to do. In your scenario, you need to force Compose to measure the size of your children to adjust the parent composable's dimensions accordingly.

This medium article gives an easier example on how to use IntrinsicSize in Compose.

Nishant Jalan
  • 844
  • 9
  • 20
  • 1
    Afaik, Intrinsic measurement are not allowed in `LazyList`, something related to this https://stackoverflow.com/questions/71080209/jetpack-compose-row-with-all-items-same-height – z.g.y Sep 26 '22 at 03:08