# onSizeChanged

Common/AllAndroid/JVM

## Functions summary

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/Modifier.https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/onSizeChanged.modifier#(androidx.compose.ui.Modifier).onSizeChanged(kotlin.Function1)(onSizeChanged: (https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/IntSize) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)` Invoked with the size of the modified Compose UI element when the element is first measured or when the size of the element changes. | Cmn |

## Functions

### Modifier.onSizeChanged

Cmn Artifact: [androidx.compose.ui:ui](https://developer.android.com/jetpack/androidx/releases/compose-ui) [View Source](https://cs.android.com/search?q=file:androidx/compose/ui/layout/OnRemeasuredModifier.kt+function:onSizeChanged) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.0.0)

```
fun Modifier.onSizeChanged(onSizeChanged: (IntSize) -> Unit): Modifier
```

Invoked with the size of the modified Compose UI element when the element is first measured or when the size of the element changes.

There are no guarantees `onSizeChanged` will not be re-invoked with the same size.

Using the `onSizeChanged` size value in a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/MutableState` to update layout causes the new size value to be read and the layout to be recomposed in the succeeding frame, resulting in a one frame lag.

You can use `onSizeChanged` to affect drawing operations. Use `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/Layout.composable#Layout(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.ui.layout.MeasurePolicy)` or `https://developer.android.com/reference/kotlin/androidx/compose/ui/layout/SubcomposeLayout.composable#SubcomposeLayout(androidx.compose.ui.Modifier,kotlin.Function2)` to enable the size of one component to affect the size of another.

Example usage:

```kotlin
import androidx.compose.material3.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.onSizeChanged

// Use onSizeChanged() for diagnostics. Use Layout or SubcomposeLayout if you want
// to use the size of one component to affect the size of another component.
Text(
    "Hello $name",
    Modifier.onSizeChanged { size -> println("The size of the Text in pixels is $size") },
)
```