Is there a way I can get the screen height in pixe...
# compose
a
Is there a way I can get the screen height in pixels,
LocalConfiguration.current.screenHeightDp.dp.toPx()
is doing some rounding and I get errors when comparing with
Offset
from
LayoutCoordinates
.
a
Can I ask what you’re trying to use the “screen height” for? And which offset you’re using from
LayoutCoordinates
? There’s some tricky subtlety there that you might be running into
a
I have a custom bottom sheet scaffold and the draggable bottomsheet part calculates its
bottomSheetOffset
using
onPlaced
and pass to the scaffold
Copy code
bottomSheet = {
    Surface(
        swipeable
            .fillMaxWidth()
            .onPlaced {
                bottomSheetOffset =
                    it.positionInParent()
            },
        shape = sheetShape,
        elevation = sheetElevation,
        color = sheetBackgroundColor,
        contentColor = sheetContentColor,
        content = {
            scaffoldState.bottomSheetState.sheetContent()
        }
    )
},
and in scaffold, I calculate padding in pixels for main content as
LocalConfiguration.current.screenHeightDp.dp.toPx() - bottomSheetOffset.y
On Pixel 6. when bottom sheet is fully collapsed, I get
bottomSheetOffset.y
as 2274.0 and
LocalConfiguration.current.screenHeightDp.dp.toPx()
as 2273.25
a
If you’re using
positionInParent
, can you use the parent size directly? That’s going to be the more directly meaningful number than the size reported by the configuration.
Depending on the “screen height” means that your bottom sheet scaffold can only be used correctly when the bottom sheet scaffold’s exactly matches the “screen height”. What’s more relevant in this case is how big your bottom sheet scaffold actually is, and that would also allow your scaffold to only occupy part of the screen and still work correctly. I’ve been putting “screen height” in quotations because there’s a few different interpretations. If you’re in multi-window mode or have a freeform window, the physical display size is irrelevant: what matters is the window size. And
LocalConfiguration.current.screenHeightDp
can also be different from the actual window size because of insets.
a
Thanks a lot, parent window size is more accurate in my case, and that is reported as 2274.0 correctly.
a
Was that with
it.parentCoordinates.size
?
a
It was with
Copy code
it.parentLayoutCoordinates?.size?.height?.toFloat()!!
parentCoordinates
is reporting
Copy code
it.parentLayoutCoordinates?.size - it.positionInParent().size
a
Great to hear! Just wanted to make sure the solution was captured for others too 😄
121 Views