How to prevent a Phonegap application to switch to landscape view
Asked Answered
H

4

23

This one should be very simple, however it's not. I'm trying to prevent my application from going into landscape view so I've changed the config.xml file like this: <preference name="orientation" value="portrait" />. Unfortunately, it's not working. Any ideas?

Happ answered 15/5, 2014 at 9:11 Comment(1)
im doing this in 2019 and <preference name="Orientation" value="landscape" /> works fine in debug build, "cordova run android" on an old Moto G4 phone plugged into usbDroughty
H
36

Ok, so I got it: The reason Phonegap ignored the <preference name="orientation" value="portrait" /> in the config.xml file is because my app was in debug mode. once I created a release version it stopped from going to landscape.

Happ answered 17/5, 2014 at 8:37 Comment(0)
B
9

Small piece of code and here you go. Try it.

<preference name="orientation" value="portrait" />
Barros answered 15/8, 2015 at 23:40 Comment(0)
P
8

hello you can install this plugin https://github.com/gbenvenuti/cordova-plugin-screen-orientation

and add this function on you index.js or one of your first executed js document

window.addEventListener("orientationchange", function(){
    screen.lockOrientation('portrait');
});

thank u

Psychokinesis answered 13/3, 2016 at 17:45 Comment(1)
Thanks - I just put the screen.lockOrientation command in a platform ready callback.Grenadines
H
-3

For platform android just set prefered orientation in androidManifest.xml like this

 <activity
        android:name="com.payoda.Insurance.SplashScreenActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Black.NoTitleBar"
    >
    </activity>

For IOS the following link may help you

Force portrait orientation on iPhone with phonegap

Highwrought answered 15/5, 2014 at 10:6 Comment(9)
How do I get to the androidManifest.xml file? I've seen this answer before but I couldn't find how to add or edit this file.Happ
It wil be included in your project structureHighwrought
androidManifest.xml is in /platforms/android/Latifundium
I don't have this path. I'm building my project at build.phonegap.com with my github repository, is that why I don't have this folder?Happ
Then you might find this blog useful pervasivecode.blogspot.in/2011/11/…Highwrought
Phonegap usually autogenerates androidmanifest based on your config file..it resides under your res or android folder depending on version you use..but if you are using phonegap site to generate apk you might nt have access to it. The above link will be usefullHighwrought
When you buid project via cli..if will fall in your project structure and you can edit it.. but in phonegap build it falls inside your apkHighwrought
#7483100 here an another link you may or maynot find it usefullHighwrought
Ok, so I decompiled my apk and I've opened the androidManifest.xml, however, the line : android:screenOrientation="portrait" was already there. I tried to add it anyway in a new activity but unfortunately, the recompiled apk cannot be installed on my phone (regardless to the manifest edit, it's something to do with the decompile and recompile process). Anyway, since the orientation definition was already there, I think my problem lies somewhere else.Happ

© 2022 - 2024 — McMap. All rights reserved.