Hot to get MeasuredWitdh and MeasuredHeight of a Composable component?

Viewed 1878

Let's suppose I have the following composable:

Column(Modifier.fillMaxWidth()) {
        
    Text()
    Text()
    Text()

}

I would like to know how I can capture the values of measuredWidth and measuredHeight of Column . Using Android View framework style, I could take advantages of the method onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) from View.

2 Answers
var width = 0
var hegiht = 0
Column(modifier = Modifier.onGloballyPositioned { layoutCoordinates ->
    width = layoutCoordinates.size.width
    hegiht = layoutCoordinates.size.height
}) {
    // Column body
}

You can use DrawCacheModifier to find the size of the composable:

Column(
    Modifier
    .fillMaxWidth()
    .then(object : DrawCacheModifier {
        override fun onBuildCache(size: Size, density: Density) {
          println("Size is $size")
       }
        override fun ContentDrawScope.draw() {
          drawContent()
    }

 })
)
Related