naresh naresh - 5 months ago 46
Android Question

How to set entire application in portrait mode only?

How do I set it so the application is running in portrait mode only? I want the landscape mode to be disabled while the application is running. How do I do it programmatically?


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:

public void onCreate(Bundle savedInstanceState) {

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:

public void onCreate() {
    registerActivityLifecycleCallbacks(new ActivityLifecycleAdapter() {
        public void onActivityCreated(Activity a, Bundle savedInstanceState) {

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).