https://kotlinlang.org logo
c

Colton Idle

07/23/2021, 2:25 AM
How do I get the size of a composable AFTER it's been laid out and measured? i.e. I want to know that my image is laid out on the screen at a final resolution of 200px x 400px.
In standard android-view land, if I wanted to get the size of an imageView being laid out I would do something like this
Copy code
imageView.viewTreeObserver.addOnPreDrawListener(
       object : ViewTreeObserver.OnPreDrawListener {
           override fun onPreDraw(): Boolean {
               viewTreeObserver.removeOnPreDrawListener(this)
               //get size of view and do something with it.
               return true
    }
})
android ktx made this simpler by adding this
Copy code
view.doOnPreDraw { 
//get size of view and do something with it.
}
how do I grab this in compose though, without potentially grabbing a size of 0 because it hasn't been laid out?
a

Albert Chang

07/23/2021, 2:37 AM
Modifier.onSizeChanged()
☝️ 1
r

romainguy

07/23/2021, 2:39 AM
@Colton Idle from Android-KTX,
doOnLayout()
is more appropriate
c

Colton Idle

07/23/2021, 3:17 AM
Whoops. @romainguy that's what I meant. @Albert Chang thanks. I will try that.
2 Views