https://kotlinlang.org logo
#compose
Title
# compose
s

Stylianos Gakis

03/05/2023, 12:05 AM
Is there a way to get the right
WindowSizeClass
when inside a composable preview? If for example in preview I’ve set
device = "spec:width=Xdp,height=Ydp,dpi=240",
which would end up making it look like a wide tablet, can I somehow get the WindowSizeClass to look at those values instead? Right now I am in reality making some decisions which render the UI differently in such screens, but in the previews this is not reflected since I have to construct one of these objects manually, like
WindowSizeClass.calculateFromSize(DpSize(500.dp, 300.dp)),
which obviously don’t match what the preview actually shows.
a

Albert Chang

03/05/2023, 2:23 PM
You can use
screenWidthDp
and
screenHeightDp
from
LocalConfiguration.current
. They should be good enough for previews.
s

Stylianos Gakis

03/05/2023, 8:24 PM
Yup, this seems to do exactly what I want, thanks!
11 Views