34

I started looking into PhoneGap yesterday and created a simple "marble" rolling around while tilting the phone. I am currently developing on Android but I want the orientation to stay as landscaping instead of moving when the phone gets spun around. Is there a way of doing this?

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.phonegap.helloworld"
    android:versionCode="1"
    android:versionName="1.0" >
<supports-screens
    android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:resizeable="true"
    android:anyDensity="true"
/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-sdk android:minSdkVersion="8" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".App" 
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
            android:name="com.phonegap.DroidGap" 
            android:label="@string/app_name" 
            android:screenOrientation="landscape"
            android:configChanges="orientation|keyboardHidden"> 
            <intent-filter></intent-filter> 
        </activity> 
    </application>
</manifest>

Code:

package com.phonegap.helloworld;

import android.os.Bundle;
import org.apache.cordova.*;

public class App extends DroidGap {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");
    }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Phil Jackson
  • 10,238
  • 23
  • 96
  • 130
  • After Apache Cordova 3.0 release modifying the AndroidManifest.xml will break the Cordova development workflow. Use a plugins instead. See possible duplicated response here: http://stackoverflow.com/questions/20294090/cordova-3-1-oriention-not-working/21165941#21165941 – sgimeno Jan 16 '14 at 15:40

5 Answers5

45

Have you tried this?

android:screenOrientation="portrait"

Inside the AndroidManifest.xml file, where you have declared your activity, just add the above line.

For example:

<activity
            android:name=".Activity.SplashScreenActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action
                    android:name="android.intent.action.MAIN" />
                <category
                    android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
shybovycha
  • 11,556
  • 6
  • 52
  • 82
mayur rahatekar
  • 4,410
  • 12
  • 37
  • 51
31

The correct way to do this for all devices is to configure config.xml

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

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

https://build.phonegap.com/docs/config-xml

Anton
  • 927
  • 10
  • 15
  • 7
    Phonegap seems to be ignoring these settings for me, regardless of their value. – apanosa Nov 26 '13 at 23:26
  • Ideally, this would work perfectly across all PhoneGap platforms. My theory is that the PhoneGap WebView may be locked to a certain orientation, but its containing Activity rotates according to the properties in AndroidManifest.xml. However, I still think this could be fixed by PhoneGap; PhoneGap could edit AndroidManifest.xml, or the DroidGap class could manage/prevent rotation. – Chad Schultz Dec 16 '13 at 18:04
  • 1
    @apanosa: this solution regard the phonegap build. I confirm that it does work on ios 7.x and android 2.x right now with phonegap 3.4.0. – cherouvim Jul 22 '14 at 09:20
  • This is right way to define orientatian. Thanks Anton – mb.akturk Feb 06 '15 at 21:19
7

It is very simple, you need to add android:screenOrientation="landscape" in your AndroidManifest.xml file.

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".LogUploaderActivity" android:label="Log Uploader" android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
Lucifer
  • 29,392
  • 25
  • 90
  • 143
3

Please try this in your activity. Inside onCreate() and before setContentView.

// keeps the screen orientation in Landscape mode
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Lucifer
  • 29,392
  • 25
  • 90
  • 143
jyotiprakash
  • 2,086
  • 1
  • 20
  • 26
1

Maybe important to mention because i did not know it:

If you use the online phonegap apk converter with hydration enabled to your application, u'll have to rebuild your app completly instead of just update it. This will help you also with preferences like "orientation" or "fullscreen" !

howard.D
  • 153
  • 7