How to restrict app to portrait mode only in ionic for all platforms?

Ionic Framework

Ionic Framework Problem Overview


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

Ionic Framework Solutions


Solution 1 - Ionic Framework

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

Solution 2 - Ionic Framework

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

  1. 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"

Solution 3 - Ionic Framework

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

Solution 4 - Ionic Framework

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" />

Solution 5 - Ionic Framework

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>

Solution 6 - Ionic Framework

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);
  
});
})

Solution 7 - Ionic Framework

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

Solution 8 - Ionic Framework

In config.xml add the following line

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

Solution 9 - Ionic Framework

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" />

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMuhammad Faizan KhanView Question on Stackoverflow
Solution 1 - Ionic FrameworkVadiem JanssensView Answer on Stackoverflow
Solution 2 - Ionic FrameworkTylerView Answer on Stackoverflow
Solution 3 - Ionic FrameworkMichael ZhouView Answer on Stackoverflow
Solution 4 - Ionic FrameworkVivek SinghView Answer on Stackoverflow
Solution 5 - Ionic FrameworkAyushKatiyarView Answer on Stackoverflow
Solution 6 - Ionic FrameworkAhmed BouchefraView Answer on Stackoverflow
Solution 7 - Ionic FrameworkVlasis PerdikidisView Answer on Stackoverflow
Solution 8 - Ionic FrameworkOmkar PorjeView Answer on Stackoverflow
Solution 9 - Ionic FrameworkBal mukund kumarView Answer on Stackoverflow