Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Android @Compose will handle screen size and orientation in Android

How Android @Compose will handle screen size and orientation in Android. I am not able to find suitable answer after googling. Can someone Answer this question.

like image 672
sudhanshu Avatar asked Nov 27 '19 22:11

sudhanshu


2 Answers

You can check for orientation like this:

val configuration = LocalConfiguration.current
when(configuration.orientation) {
    Configuration.ORIENTATION_LANDSCAPE -> {}
    Configuration.ORIENTATION_PORTRAIT -> {}
    Configuration.ORIENTATION_SQUARE -> {}
    Configuration.ORIENTATION_UNDEFINED -> {}
}

For screen size:

BoxWithConstraints() {
    // thats what you can access:
    this.maxWidth
    this.maxHeight
    this.minWidth
    this.minHeight
}
like image 88
M. Wojcik Avatar answered Nov 06 '22 18:11

M. Wojcik


With 1.0.x you can use the BoxWithConstraints composable to define different ui based on the screen size.

Something like:

BoxWithConstraints {
        if (minWidth < 480.dp) {
            /* .... */
        } else if (minWidth < 720.dp) {
            /* .... */
        } else {
            /* .... */
        }
    }

To handle the orientation you can use LocalConfiguration.
Something like:

val configuration = LocalConfiguration.current
when (configuration.orientation) {
    Configuration.ORIENTATION_LANDSCAPE -> {
        /* ... */
    }
    else -> {
        /* ... */
    }
}

}

like image 41
Gabriele Mariotti Avatar answered Nov 06 '22 17:11

Gabriele Mariotti