Android: How to Programmatically Lock the Orientation

In mobile application development, there may be times when you want certain screens within your app to always show in landscape or in portrait mode. Android makes this easy to do within your Activity class:

import android.content.pm.ActivityInfo;
...
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

You can also set this in the manifest:

<activity android:name=".YourActivity"
              android:label="@string/app_name"
              android:screenOrientation="landscape">