Rene Sá
Rene Sá

Reputation: 4

How use plugin in phonegap for change screen orientation?

My phonegap app should run in portrait orientation. Only one page (index.html) should run in landscape mode. Then I found the plugin for this, but doesn't work for me.

cordova plugin add net.yoik.cordova.plugins.screenorientation

In config.xml:

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

Index.html:

<html>
<head>
<title>App</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<script>
var so = cordova.plugins.screenorientation;
so.setOrientation(so.Orientation.LANDSCAPE);
</script>

For android, the application returns an error App has stopped.

For iPhone, nothing happening.

Upvotes: 2

Views: 5527

Answers (2)

gben
gben

Reputation: 200

It looks like you are using the old API which existed prior to v1.0. Maybe you found the example on an old blog post?

In any case see the plugin documentation. Using the current API it will look like: (must be after device ready)

<script>
    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady()
    {
        screen.lockOrientation('landscape');
    }
</script>

Upvotes: 5

AtanuCSE
AtanuCSE

Reputation: 8940

Add this script first

<script type="text/javascript" src="cordova.js"></script>

Then inside another script tag

<script>
    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady()
    {

       var so = cordova.plugins.screenorientation;
       so.setOrientation(so.Orientation.LANDSCAPE);
    }
</script>

Upvotes: 1

Related Questions