Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get screen width and height in Compose Multiplatform?

How can I get the screen size in Compose Multiplatform without using expect/actual?

I have found the following explanation in https://github.com/JetBrains/compose-multiplatform/discussions/3225#discussioncomment-7195192:

You can also use a Layout as your root Composable and obtain the size from the passed constraints. This solution doesn't depend on platform implementations and also gives you the exact size given to your Composable which could be different from the device screen size in some cases.

Once you have that size can use a CompositionLocal to pass it down the composable tree

I have not been able to fully understand or successfully implement the suggested layout.

like image 446
Roy Solberg Avatar asked Feb 04 '26 20:02

Roy Solberg


1 Answers

Starting with Multiplatform Compose version 1.5.10-beta01 (https://github.com/JetBrains/compose-multiplatform/releases/tag/v1.5.10-beta01) you can use LocalWindowInfo.current.containerSize for non-android targets.

Android has the usual LocalConfiguration.current, so you can use that there.

like image 62
Jakub Kostka Avatar answered Feb 09 '26 00:02

Jakub Kostka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!