I tested the following code on a physical phone, HUAWEI GR5 KII-L21, running Android Lollipop.
@Composable
fun MainScreen() {
val metrics = LocalContext.current.resources.displayMetrics
println("the width is ${metrics.widthPixels}")
println("the height is ${metrics.heightPixels}")
val configuration = LocalConfiguration.current
println("height: ${configuration.screenHeightDp.dp}")
println("width: ${configuration.screenWidthDp.dp}")
with(LocalDensity.current) {
println("dp to px: ${210.dp.roundToPx()}")
}
BoxWithConstraints(
modifier = Modifier
.size(70.dp)
.background(color = Color.Red)
){
println(constraints)
}
}
The result in the logcat is:
I the width is 1080
I the height is 1776
I height: 568.0.dp
I width: 360.0.dp
I dp to px: 630
I Constraints(minWidth = 210, maxWidth = 210, minHeight = 210, maxHeight = 210)
First of all, this phone is 5.5'' at 1920x1080. Compose reports 1776x1080.
Second, Compose reports a width in dp of 360
, but Boxwithconstraints
receives a maxWidth of 210
only. I expect it to pass 360
to Boxwithconstraints
This, when converted to px, does not match 1080
, the physical screen width. Could you shed some light on this?