Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set entire application in portrait mode only?

Tags:

android

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?

like image 359
naresh Avatar asked Jul 19 '11 10:07

naresh


People also ask

How do I make my android apps portrait only?

If you want to develop an application in portrait mode, add screenOrientation inside application tag. In the above result it is showing only portrait mode. now turn your device it not going to change the view according to orientation.

How do I force an app into portrait mode?

On the main screen of Rotation Manager, select an orientation by tapping on either the vertical or horizontal icons next to a specific app to lock it into either landscape or portrait mode. Highlighting both icons will allow that particular app to auto-rotate.


13 Answers

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

like image 131
Vincent Mimoun-Prat Avatar answered Oct 19 '22 00:10

Vincent Mimoun-Prat


Yes you can do this both programmatically and for all your activities making an AbstractActivity that all your activities extends.

public abstract class AbstractActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

This abstract activity can also be used for a global menu.

like image 44
Jan-Terje Sørensen Avatar answered Oct 19 '22 00:10

Jan-Terje Sørensen


You can do this for your entire application without having to make all your activities extend a common base class.

The trick is first to make sure you include an Application subclass in your project. In its onCreate(), called when your app first starts up, you register an ActivityLifecycleCallbacks object (API level 14+) to receive notifications of activity lifecycle events.

This gives you the opportunity to execute your own code whenever any activity in your app is started (or stopped, or resumed, or whatever). At this point you can call setRequestedOrientation() on the newly created activity.

class MyApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();  

        // register to be informed of activities starting up
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {

            @Override
            public void onActivityCreated(Activity activity, 
                                          Bundle savedInstanceState) {

                // new activity created; force its orientation to portrait
                activity.setRequestedOrientation(
                    ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

            }

            ....

        });

    }
}
like image 43
Graham Borland Avatar answered Oct 19 '22 00:10

Graham Borland


You can set this in your manifest file..

android:name=".your launching activity name"
android:screenOrientation="portrait"

and you can also achive the same by writing the code in your class file like:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
like image 34
SRam Avatar answered Oct 19 '22 00:10

SRam


Add android:screenOrientation="portrait" to the activity in the AndroidManifest.xml. For example:

<activity android:name=".SomeActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait">
like image 20
Nikunj Patel Avatar answered Oct 18 '22 23:10

Nikunj Patel


Use:

android:screenOrientation="portrait" 

Just write this line in your application's manifest file in each activity which you want to show in portrait mode only.

like image 40
Deepak Sharma Avatar answered Oct 19 '22 00:10

Deepak Sharma


Write this to your manifest file, for every activity:

android:screenOrientation="portrait" 
like image 40
Zwiebel Avatar answered Oct 19 '22 00:10

Zwiebel


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    //setting screen orientation locked so it will be acting as potrait
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
}
like image 33
Emon Hossain Munna Avatar answered Oct 18 '22 22:10

Emon Hossain Munna


As from Android developer guide :

"orientation" The screen orientation has changed — the user has rotated the device. Note: If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations.

"screenSize" The current available screen size has changed. This represents a change in the currently available size, relative to the current aspect ratio, so will change when the user switches between landscape and portrait. However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device). Added in API level 13.

So, in the AndroidManifest.xml file, we can put:

<activity
            android:name=".activities.role_activity.GeneralViewPagerActivity"
            android:label="@string/title_activity_general_view_pager"
            android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden|screenSize"
            >
        </activity> 
like image 36
super1ha1 Avatar answered Oct 18 '22 23:10

super1ha1


Adding <preference name="orientation" value="portrait" /> under <widget> in my config.xml worked for me.

(The other solutions either didn't work on my device, were overwritten during building or gave deprecation errors during the build process.)

like image 20
Philip Avatar answered Oct 18 '22 23:10

Philip


in Manifest file which activity you want to use in "portrait" you must write these code in Activity tag

  android:screenOrientation="portrait" 

like this

         android:icon="@drawable/icon"
        android:name="com.zemkoapps.hd.wallpaper.AndroidGridLayoutActivity" 
        android:screenOrientation="portrait" >

but if u want screen in landscape use this code like this

android:screenOrientation="landscape"
like image 30
Bilal Jarral Avatar answered Oct 19 '22 00:10

Bilal Jarral


If anyone was wondering , how you could do this for your entire application without having to make all your activities extend a common base class in Kotlin , see the example below :

class InteractiveStoryApplication: Application() {
override fun onCreate() {
    super.onCreate()
    registerActivityLifecycleCallbacks(object: ActivityLifecycleCallbacks {
        override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
            activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }

        override fun onActivityPaused(activity: Activity?) {
        }

        override fun onActivityResumed(activity: Activity?) {
        }

        override fun onActivityDestroyed(activity: Activity?) {
        }

        override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {
        }

        override fun onActivityStarted(activity: Activity?) {
        }

        override fun onActivityStopped(activity: Activity?) {
        }
    })
}
}

and then you have to add your common base class in AndroidManifest like so:

<application android:allowBackup="true"
android:name=".InteractiveStoryApplication"
like image 43
Ashildr Avatar answered Oct 18 '22 23:10

Ashildr


You can do it in two ways .

  1. Add android:screenOrientation="portrait" on your manifest file to the corresponding activity
  2. Add setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); to your activity in `onCreate() method
like image 45
Saneesh Avatar answered Oct 18 '22 23:10

Saneesh