Start native apps faster with the Composables CLI ->

Window Container Size

A Composable function that returns the current window size and automatically triggers recomposition when the window is resized, enabling responsive layouts.

API Reference

Installation

implementation("com.composables:composeunstyled-window-container-size")

Code Examples

Basic Example

Use currentWindowContainerSize() to get the current window dimensions:

View on GitHub
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.composeunstyled.currentWindowContainerSize

@Composable
fun WindowContainerSizeDemo() {
  val windowContainerSize = currentWindowContainerSize()

  Column(
    modifier = Modifier.fillMaxSize()
      .background(Color.White)
      .padding(24.dp),
    horizontalAlignment = Alignment.CenterHorizontally,
    verticalArrangement = Arrangement.Center,
  ) {
    BasicText(
      text = "Window container size",
      style = TextStyle(
        color = Color.Black,
        fontSize = 22.sp,
        lineHeight = 28.sp,
        fontWeight = FontWeight.Medium,
      ),
    )
    BasicText(
      text = "${windowContainerSize.width} x ${windowContainerSize.height}",
      style = TextStyle(
        color = Color.Black,
        fontSize = 36.sp,
        lineHeight = 44.sp,
      ),
      modifier = Modifier.padding(top = 12.dp),
    )
    BasicText(
      text = "Resize the window to watch this value update.",
      style = TextStyle(
        color = Color(0xFF525252),
        fontSize = 14.sp,
        lineHeight = 20.sp,
      ),
      modifier = Modifier.padding(top = 8.dp),
    )
  }
}
HINT: Resize your browser's width to see the size changing.
import com.composeunstyled.currentWindowContainerSize
val containerSize = currentWindowContainerSize()