3

I am using phonegap and jquery mobile to build an app for an android phone. Is there a possibility to lock the orientation on one page? E.g. page "map" is loaded and the orientation is locked to "landscape" mode.

peterh
  • 11,875
  • 18
  • 85
  • 108
Siggy Petersen
  • 195
  • 3
  • 6
  • 16

2 Answers2

8

I have tried to modify manifest.xml and it works. Simply add android:screenOrientation="landscape" attribute to your activity tag like below:

<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
    <activity android:label="@string/app_name" android:name=".Phonegap_AppName"
        android:configChanges="orientation|keyboardHidden" android:screenOrientation="landscape">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
radekEm
  • 4,617
  • 6
  • 32
  • 45
5

Not really i think. In xCode for iOS apps is it not possible. The only fix I can come up with, is to rotate your body or a wrapper acording to window.orientation

$(window).bind("orientationchange", function(){
    var orientation = window.orientation;
    var new_orientation = (orientation) ? 0 : 180 + orientation;
    $('body').css({
        "-webkit-transform": "rotate(" + new_orientation + "deg)"
    });
});

This is a risky way but thinks its the only way..

Hope this helps, please let me know !

See: http://jsfiddle.net/aalouv/sABRQ/1/


Alternative you can bind to the window resize event.

$(window).bind("resize", function(){
    var orientation = window.orientation;
    var new_orientation = (orientation) ? 0 : 180 + orientation;
    $('body').css({
        "-webkit-transform": "rotate(" + new_orientation + "deg)"
    });
});

I wrote this little script a while ago: It fix the multiply resize callback bug in iOS safari and the non-/late-/early-trigger(1) orientationchange bug in android.

(1) Sometimes it dosn't trigger sometimes before and some times after the browser has changes width + height? Wired!

if (!(/iphone|ipad/gi).test(navigator.appVersion)) {
    $(window).unbind("resize").bind("resize", function() {
        $(window).trigger("orientationchange");
    });
}

If not iphone or ipad you are triggering the orientationchange event on the window.

So when you will bind any function the the resize you do it throw orientationchange.

Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
  • Hey Andreas Al thanks for reply, i try to use your solution but in fact i does not come into the binded function even if i rotate the screen. I also did it with the `.live` function of jQuery but it still not works. – Siggy Petersen Aug 10 '11 at 11:42
  • Ahh ok maybe is has to be bounded to the document and not to the window. See edit above – Andreas Louv Aug 10 '11 at 11:44
  • Its for android! I have has some problem with the orientationchange myself on android. I still thing you can use resize and archive the same result. But beware on iOS resize get trigged up to 10 times on every orientationchange. – Andreas Louv Aug 10 '11 at 11:51
  • Ok perfect with the "resize" type it works, but now my header is jumping around. How Can i fix it only on the "landscape" mode? – Siggy Petersen Aug 10 '11 at 12:03
  • Maybe wrap all of your stuff in a div tag and and instead of rotation body rotate the div. Think about position absolute and so on... – Andreas Louv Aug 10 '11 at 12:07
  • Ok i worked with a few if-statements for change rotation and it works good, but the whole jquery mobile ui get crashed so i decided to abstain for the locking. I hope jquery mobile will provide some functionallity in the future for this. But thank you for your support Andreas – Siggy Petersen Aug 10 '11 at 12:27