Search code examples
androidandroid-layoutandroid-jetpack-compose

How to get MeasuredWitdh and MeasuredHeight of a Composable component?


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.


Solution

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