Window Container Size
A Composable function that returns the current window size and automatically triggers recomposition when the window is resized, enabling responsive layouts.
View MarkdownInstallation
implementation("com.composables:composeunstyled-window-container-size:2.10.0")Code Examples
Basic Example
Use currentWindowContainerSize() to get the current window dimensions:
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.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.composeunstyled.Text
import com.composeunstyled.currentWindowContainerSize
import com.composeunstyled.demo.colors
import com.composeunstyled.demo.contentToken
import com.composeunstyled.demo.mutedContentToken
import com.composeunstyled.theme.Theme
@Preview
@Composable
fun WindowContainerSizeDemo() {
val windowContainerSize = currentWindowContainerSize()
Column(
modifier = Modifier.fillMaxSize()
.padding(24.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
) {
Text(
text = "Window container size",
color = Theme[colors][contentToken],
fontSize = 22.sp,
lineHeight = 28.sp,
fontWeight = FontWeight.Medium,
)
Text(
text = "${windowContainerSize.width} x ${windowContainerSize.height}",
color = Theme[colors][contentToken],
fontSize = 36.sp,
lineHeight = 44.sp,
modifier = Modifier.padding(top = 12.dp),
)
Text(
text = "Resize the window to watch this value update.",
color = Theme[colors][mutedContentToken],
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.currentWindowContainerSizeval containerSize = currentWindowContainerSize()