views:

4995

answers:

3

I know that in Safari on an iPhone you can detect the screens orientation and change of orientation by listening for the onorientationchange event and querying window.orientation for the angle.

Is this possible in the browser on Android phones?

Update

To be clear, I am asking whether the rotation of an android device can be detected by JavaScript running on a standard web page. It is possible on an iPhone and I wondered whether it could be done for Dndroid phones.

Thanks

+1  A: 

You might want to check out this site: http://androidguys.com/?s=rotational+forces&x=9&y=9.

Shawn Steward
Thanks, but those articles only cover Android applications. I am asking whether JavaScript on a web page can detect the rotation like an iPhone can.
Phil
+6  A: 

You could always listen to the window resize event. If, on that event, the window went from being taller than it is wide to wider than it is tall (or vice versa), you can be pretty sure the phone orientation was just changed.

Joel Mueller
That's a good idea, I'll try that (and ask my friend with the Android phone to test!)
Phil
I've just realised that this method doesn't give me the orientation of the phone. I will know whether it is portrait or landscape, but not if it is upside down or has been turned to the left or to the right.Any further ideas?
Phil
I'm not sure how it could possibly matter... Why do you need to know if the phone is upside down? The phone's browser has a top, and the web page will be oriented to the browser's top. Any user with two brain cells to rub together who's looking at a web page upside-down will just turn their phone around if they want to see it right-side-up
Joel Mueller
That's a little harsh. I'm interested to know so that I can show different content based on the orientation of the screen, this could be up to 4 different pages, requiring the browser to know whether it had been rotated by 0, 90, 180 and 270 degrees. This is all possible, in JavaScript, on the iPhone and is why I am asking.
Phil
I'm still having a hard time picturing what a web page could usefully do differently when the device it's rendered on is rotated 270 degrees, but if you say you've got a valid use-case, I won't quibble. In that case: I'm sorry, but I have no idea if the Android browser provides this level of detail.
Joel Mueller
This is a very important question for webapps rather then websites. Native android apps do also know the orientation. Why shouldn't web apps do?
Federico Elles
I'm not saying web apps shouldn't be able to find this out! I'd just like a concrete example of a web page that's not a game that is made better by being aware of the degree rotation of the phone it's rendered on. Perhaps my imagination is poor because I don't own an iPhone or an Android, but the only thing I can think of to do with the exact degree of rotation in a web page is make a gimmicky game.
Joel Mueller
+10  A: 

The short answer is "YES". To detect an orientation change on an Android browser, add a listener for either the 'orientationchange' or 'resize' event on window:

// Detect whether device supports orientationchange event, otherwise fall back to
// the resize event.
var supportsOrientationChange = "onorientationchange" in window,
    orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";

window.addEventListener(orientationEvent, function() {
    alert('HOLY ROTATING SCREENS BATMAN:' + window.orientation + " " + screen.width);
}, false);

You can then check the window.orientation property to figure out which way the device is oriented. With Android phones, screen.width or screen.height also updates as the device is rotated. (this is not the case with the iPhone).

jb
This doesn't appear to work on a Nexus One
Jeremy
updated the code for nexus one
jb
does this really work? i tried jquery $(window).bind("orientationchange",fn); but it didnt work, do i have to use the form above? i tried "onorientationchange" in window, it retuns false
Ayyash
It works well. Ayyash - the whole point here that when "orientationchange" is not supported it will fall back to "resize"
Dror