Tech Notes

Prevent Landscape Orientation in Android Apps

One line of code to lock an Android activity to portrait orientation.

The following line of code prevents your Android app from switching to landscape orientation. I’m posting it mainly for my own future reference, but it may be useful to you too.

Place the code below in your activity’s onCreate method:

this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

Alternatively, you can set android:screenOrientation="portrait" on the activity in your AndroidManifest.xml.