Solved

Detect orientation using javascript on android devices

Posted on 2012-04-12
2
1,404 Views
Last Modified: 2012-06-27
Using some code from this question I have set up some code to detect when an android device is rotated. It works great for the asus tablet (4.0.3) and two simulators (4.0.3 and 2.1), but for the kindle fire (2.3.4), and droidx (2.3.4) it switches the width and height.

Code:

<script type="text/javascript">
    var supportsOrientationChange = "onorientationchange" in window,orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";

    window.addEventListener(orientationEvent, function() {
        alert("The rotation is " + window.orientation + " and the resolution is " + screen.width + " x " + screen.height);
        modRule();
    }, false);
</script>

Open in new window


Output from asus tablet

Holding it in what looks like landscape:

The rotation is 0 and the resolution is 1280 x 800

Portrait

The rotation is -90 and the resolution is 800 x 1280

Output from Kindle Fire

Landscape

The rotation is 90 and the resolution is 600 x 819

Portrait:

The rotation is 0 and the resolution is 1024 x 395

output from droidx

landscape:

The rotation is 90 and the resolution is 320x488

Portrait:

The rotation is 0 and the resolution is 569x239

Is there a way I can

a) Make the javascript detect if it should use height instead of width or width instead of height

OR

b) Make the devices report the correct values for their width and height?
0
Comment
Question by:HDM
2 Comments
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 37841973
Did you follow the links in the comment?

Android 2.2 and 2.3 have a bug such that screen width and height are not reported reliably. See http://stackoverflow.com/q/5021090/436641 and http://stackoverflow.com/q/3702073/436641.
0
 

Author Closing Comment

by:HDM
ID: 37844288
After I saw those links, I wound up modifying my app so it sends the sdk version in the user agent and then the javascript checks the useragent for android 10 (the sdk for 2.3.4) and if it's a match, it switches the width and height. It's a mess, but works for now.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
DTD and JAVA versions 1 32
modify change color of text 9 37
javascript and jquery question 4 24
What is the correct way to check for empty value in input field 2 18
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question