Maiko Trindade
Maiko Trindade

Reputation: 3059

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.

Upvotes: 4

Views: 3870

Answers (2)

Mohammad Sianaki
Mohammad Sianaki

Reputation: 1761

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

Upvotes: 12

deaddroid
deaddroid

Reputation: 430

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()
    }

 })
)

Upvotes: 0

Related Questions