How to set entire application in portrait mode only?

For any Android version

From XML

You can specify android:screenOrientation="portrait" for each activity in your manifest.xml file. You cannot specify this option on the application tag.

From Java

Other option is to do it programmatically, for example in an Activity base class:

@Override
public void onCreate(Bundle savedInstanceState) {
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

For Android 4+ (API 14+)

Last option is to do it with activity lifecycle listeners which is only available since Android 4.0 (API 14+). Everything happens in a custom Application class:

@Override
public void onCreate() {
    super.onCreate();  
    registerActivityLifecycleCallbacks(new ActivityLifecycleAdapter() {
        @Override
        public void onActivityCreated(Activity a, Bundle savedInstanceState) {
            a.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    });
}

ActivityLifecycleAdapter is just a helper class you’ll need to create which will be an empty implementation of ActivityLifecycleCallbacks (so you don’t have to override each and every methods of that interface when you simply need one of them).

Leave a Comment