134

I'm working on Ionic/Cordova, I added this to androidManifest.xml but this didn't work for me and app is still showing in both ways

android:screenOrientation="portrait"

How can I restrict my app to portrait mode only? I have checked on android and it doesn't work

Phonolog
  • 6,321
  • 3
  • 36
  • 64
Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186

10 Answers10

328

If you want to restrict to portrait mode only on all devices you should add this line to the config.xml in your projects root folder.

<preference name="orientation" value="portrait" />

After that, please rebuild the platform by typing below text in command line:

ionic build
Vadiem Janssens
  • 4,069
  • 1
  • 17
  • 27
37

Ionic v2+ Update

For Ionic versions 2 and later, you will need to also install the corresponding Ionic Native package for any plugin you use, including cordova-plugin- and phonegap-plugin- plugins.

  1. Install Ionic Native Plugin

    Install Ionic Native's TypeScript module for the plugin from the CLI.

    $ ionic cordova plugin add --save cordova-plugin-screen-orientation
    $ npm install --save @ionic-native/screen-orientation
    

    * Note that the --save command is optional and can be omitted if you do not wish to add the plugin to your package.json file

  2. Import ScreenOrientation Plugin

    Import plugin into your controller, more details available in the documentation.

    import { ScreenOrientation } from '@ionic-native/screen-orientation';
    
    @Component({
        templateUrl: 'app.html',
        providers: [
            ScreenOrientation
        ]
    })
    
    constructor(private screenOrientation: ScreenOrientation) {
      // Your code here...
    }
    
  3. Do Your Thing

    Lock and unlock the screen orientation programatically.

    // Set orientation to portrait
    this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.PORTRAIT);
    
    // Disable orientation lock
    this.screenOrientation.unlock();
    
  4. Bonus Points

    You can also get the current orientation.

    // Get current orientation
    console.log(this.screenOrientation.type);  // Will return landscape" or "portrait"
    
Rockin4Life33
  • 2,240
  • 1
  • 31
  • 29
Tyler
  • 3,616
  • 3
  • 22
  • 34
  • 1
    NOTE: you need "@ionic-native/core": "^3.6.1", because lower version causes error: https://forum.ionicframework.com/t/ionicnativeplugin-missing-from-ionic-native-core/88019 – Luckylooke Jun 22 '17 at 11:55
  • Try adding ScreenOrientation to the list of providers – Tyler Aug 07 '17 at 16:49
  • 3
    `ionic plugin` no longer seems to work. I think it is now `ionic cordova plugin`. Such as `ionic cordova plugin add --save cordova-plugin-screen-orientation` – Rockin4Life33 Oct 28 '17 at 21:23
  • 2
    adding `` seems to be enough for me – tobbe Jan 25 '18 at 12:04
  • Is it possible that this won't work on the IonicDev App environment? I followed the instructions and the app won't lock the screen orientation. – nicozica Aug 10 '18 at 17:59
  • 2
    DevApp is just running your app as a webview within the DevApp app, so some native features do not work. Try compiling and running your app directly on the device and it should work. – Tyler Aug 10 '18 at 21:05
14

According to https://cordova.apache.org/docs/en/4.0.0/config_ref/#global-preferences,

Orientation allows you to lock orientation and prevent the interface from rotating in response to changes in orientation. Possible values are default, landscape, or portrait. Example:

<preference name="Orientation" value="landscape" />

Be aware that these values are case sensitive, it is "Orientation", not "orientation".

Michael Zhou
  • 151
  • 1
  • 4
  • 1
    When you scroll up on the link you provided, it states the following: "The tag sets various options as pairs of name/value attributes. Each preference's name is *case-insensitive*. [...]." – Vadiem Janssens Apr 29 '16 at 08:15
  • 2
    "orientation" works for me. "Orientation" gives me white screen when running the app on actual device. – CMA Jul 22 '16 at 10:59
  • 2
    "orientation" works for me with Ionic 2 in an android device. – kamayd Oct 05 '16 at 23:11
6

If you want to do something on your orientation means you want to perform any action when change your app orientation then you have to use ionic framework plugin... https://ionicframework.com/docs/native/screen-orientation/

If you just want to restrict your app on portrait or landscape mode then you have to just add these following line in your config.xml

<preference name="orientation" value="portrait" />
                 OR
<preference name="orientation" value="landscape" />
Vivek Singh
  • 521
  • 6
  • 9
6

Please add android:screenOrientation="portrait" in the androidManifest.xml file as an attribute of activity tag for android.

<activity
        android:name="com.example.demo_spinner.MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
</activity>
AyushKatiyar
  • 1,000
  • 8
  • 15
4

First, you need to add the Cordova Screen Orientation Plugin using

cordova plugin add cordova-plugin-screen-orientation

and then add screen.lockOrientation('portrait'); to .run() method

angular.module('myApp', [])
.run(function($ionicPlatform) {

    screen.lockOrientation('portrait');
    console.log('Orientation is ' + screen.orientation);

});
})
Devid Farinelli
  • 7,514
  • 9
  • 42
  • 73
Ahmed Bouchefra
  • 256
  • 2
  • 4
2

Inside your angular app.js add the line screen.lockOrientation('portrait'); like shown bellow:

an

angular.module('app', ['ionic'])
.run(function($ionicPlatform) {
    // LOCK ORIENTATION TO PORTRAIT.
    screen.lockOrientation('portrait');
  });
})

You will also have other code in the .run function, but the one you are interested in is the line I wrote. I haven't added the line <preference name="orientation" value="portrait" /> in my code, but you may need to add the cordova-plugin-device-orientation

Devid Farinelli
  • 7,514
  • 9
  • 42
  • 73
1

In config.xml add the following line

<preference name="orientation" value="portrait" />
Omkar Porje
  • 278
  • 1
  • 5
1

Using ionic 7:

If you only want to lock orientation and won't use it for dynamic functionality in your app. You don't need to install any package but just to update the configuration.

  • On iOS

in Info.plist file edit value on key UISupportedInterfaceOrientations and leave only UIInterfaceOrientationPortrait. Like that:

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
</array>
  • On Android

In AndroidManifest.xml update activity tag and add:

  android:screenOrientation="portrait"
stanimirsp
  • 2,548
  • 2
  • 26
  • 36
  • Hey @stanimirsp, do you know why using this approach for iOS I get this error? At least on Ionic 5: `The "UIInterfaceOrientationPortrait,UIInterfaceOrientationPortraitUpsideDown" orientations were provided for the UISupportedInterfaceOrientations Info.plist key in the com.moodle.aprende bundle, but you need to include all of the "UIInterfaceOrientationPortrait,UIInterfaceOrientationPortraitUpsideDown,UIInterfaceOrientationLandscapeLeft,UIInterfaceOrientationLandscapeRight" orientations to support iPad multitasking` – Aldo Fernando Saia Aug 28 '23 at 12:25
  • @AldoFernandoSaia, portrait mode is only for iPhone. For iPad in Info.plist must be with all orientations. Check [this](https://snappify.com/view/31dd4e3a-ffb0-447e-9fdb-c06164ee0fd7) example – stanimirsp Aug 28 '23 at 13:44
0

You can try this:-

Cordova plugin to set/lock the screen orientation in a common way for iOS, Android, WP8 and Blackberry 10. This plugin is based on an early version of Screen Orientation API so the api does not currently match the current spec.

https://github.com/apache/cordova-plugin-screen-orientation

or try this code in xml config:-

<preference name="orientation" value="portrait" />