Jetpack Compose Screen Orientation: Detect and Lock

Short answer: in Jetpack Compose you detect orientation with LocalConfiguration.current.orientation, and you lock it either in the manifest with android:screenOrientation or in code with activity.requestedOrientation. Use rememberSaveable or a ViewModel so state survives rotation.

Detect the current orientation

@Composable
fun OrientationAwareScreen() {
    val configuration = LocalConfiguration.current
    val isLandscape = configuration.orientation == Configuration.ORIENTATION_LANDSCAPE

    if (isLandscape) {
        Row(Modifier.fillMaxSize()) {
            Text("Landscape layout", Modifier.weight(1f))
            Text("Second pane", Modifier.weight(1f))
        }
    } else {
        Column(Modifier.fillMaxSize()) {
            Text("Portrait layout")
            Text("Single column")
        }
    }
}

Import android.content.res.Configuration. The composable recomposes when the configuration changes, so the layout switches automatically.

Lock orientation in the manifest

<activity
    android:name=".MainActivity"
    android:screenOrientation="portrait"
    android:exported="true" />

Common values include portrait, landscape, sensorLandscape, userPortrait and unspecified.

Lock orientation from Compose

To lock only one screen, set requestedOrientation and restore it when the screen leaves:

@Composable
fun LockScreenOrientation(orientation: Int) {
    val context = LocalContext.current
    DisposableEffect(orientation) {
        val activity = context.findActivity()
        val original = activity?.requestedOrientation
        activity?.requestedOrientation = orientation
        onDispose {
            activity?.requestedOrientation =
                original ?: ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
        }
    }
}

fun Context.findActivity(): Activity? {
    var current = this
    while (current is ContextWrapper) {
        if (current is Activity) return current
        current = current.baseContext
    }
    return null
}

// Usage inside a screen
LockScreenOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

Import android.app.Activity, android.content.Context, android.content.ContextWrapper and android.content.pm.ActivityInfo.

Keep state on rotation

By default, rotating the device recreates the Activity. Composable state created with remember is lost. Use these instead:

  • rememberSaveable for small UI state such as text field values or selected tabs.
  • A ViewModel for screen data. See our MVVM tutorial.
var text by rememberSaveable { mutableStateOf("") }

Avoid using android:configChanges to block recreation unless you really need it. It makes you responsible for handling every configuration change yourself.

Think about large screens

Locking an app to portrait can give a poor experience on tablets, foldables and desktop windows, and newer Android versions may ignore orientation locks on large screens. Prefer adaptive layouts based on window size, for example with the Material 3 window size classes, and lock orientation only for screens that truly need it, such as a camera or video player.

Common mistakes

  • Using remember for state that must survive rotation. Use rememberSaveable or a ViewModel.
  • Locking the whole app to portrait. This hurts tablet and foldable users.
  • Forgetting to restore orientation. If you lock it from Compose, restore it in onDispose.
  • Reading orientation instead of window size. Orientation does not describe available space in split-screen or multi-window mode.

FAQ

How do I get the screen orientation in Jetpack Compose?

Read LocalConfiguration.current.orientation and compare it with Configuration.ORIENTATION_LANDSCAPE or ORIENTATION_PORTRAIT.

How do I lock the screen to portrait in Compose?

Add android:screenOrientation="portrait" to the Activity in the manifest, or set requestedOrientation in code.

Why does my state reset when I rotate the screen?

The Activity is recreated and remember state is lost. Use rememberSaveable or a ViewModel.

Should I use orientation or window size classes?

Prefer window size classes for responsive layouts because they reflect the space your app actually has.

More guides: Jetpack Compose tutorials.