Screen Rotation

Author: Scott Pierce

A simple Composable to manually rotate the screen with Jetpack Compose, without rotating the Activity.

@Composable
fun RotateScreen(
    rotation: ScreenRotation,
    modifier: Modifier = Modifier,
    contentAlignment: Alignment = Alignment.TopStart,
    content: @Composable () -> Unit
) {
    BoxWithConstraints {
        val width: Dp
        val height: Dp

        when (rotation) {
            ScreenRotation.LEFT_90_DEGREES, ScreenRotation.RIGHT_90_DEGREES -> {
                width = maxHeight
                height = maxWidth
            }
            else -> {
                width = maxWidth
                height = maxHeight
            }
        }

        Box(
            modifier = modifier
                .rotate(rotation.degrees)
                .width(width)
                .height(height),
            contentAlignment = contentAlignment
        ) {
            content()
        }
    }
}

enum class ScreenRotation(val degrees: Float) {
    LEFT_90_DEGREES(-90f), RIGHT_90_DEGREES(90f), NORMAL(0f)
}

Have a project you'd like to submit? Fill this form, will ya!

If you like this snippet, you might also like:

Maker OS is an all-in-one productivity system for developers

I built Maker OS to track, manage & organize my life. Now you can do it too!