Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.orientation returns different values in iOS and Android

I am testing my Web Application on iPad (Safari browser) and Samsung Tab 2 (Default browser). The window.orientationchange returns different values in both the devices.

$(document).ready(function() {
            window.addEventListener("orientationchange", centerLoginBox);
            window.addEventListener("load", centerLoginBox);
        });

        function centerLoginBox() {
            if (window.orientation == 90 || window.orientation == -90) { //Landscape Mode
                $('#loginbox').css('margin-top', '20%');
                alert(window.orientation);
            }
            else if (window.orientation == 0 || window.orientation == 180) { //Portrait Mode
                $('#loginbox').css('margin-top', '40%');
                alert(window.orientation);
            }

In Tab 2 the alert throws '0' and '180' for landscape mode and the values '90' and '-90' for portrait mode(just the opposite behavior in iPad).

Is this some kind of design difference in iOS and Android? What could be a common solution for this issue?

like image 965
Sayan Avatar asked Dec 24 '12 10:12

Sayan


People also ask

How do I manage orientation on android?

If you want to manually handle orientation changes in your app you must declare the "orientation" , "screenSize" , and "screenLayout" values in the android:configChanges attributes. You can declare multiple configuration values in the attribute by separating them with a pipe | character.

What class ensures the dialog is displayed properly even when the user changes the device orientation?

The DialogFragment class provides all the controls you need to create your dialog and manage its appearance, instead of calling methods on the Dialog object. Using DialogFragment to manage the dialog ensures that it correctly handles lifecycle events such as when the user presses the Back button or rotates the screen.


1 Answers

Ok, this is what I did. I queried for the User Agent information and checked if the device was Android based. If so, change the conditions of window.orientation for both Portrait and Landscape mode.

CODE

function centerLoginBox() {
        var ua = navigator.userAgent.toLowerCase();
        var isAndroid = ua.indexOf("android") > -1; // Detect Android devices
        if (isAndroid) {
            //window.orientation is different for iOS and Android
            if (window.orientation == 0 || window.orientation == 180) { //Landscape Mode
                $('#loginbox').css('margin-top', '20%');
            }
            else if (window.orientation == 90 || window.orientation == -90) { //Portrait Mode
                $('#loginbox').css('margin-top', '40%');
            }
        }
        else {
            if (window.orientation == 90 || window.orientation == -90) { //Landscape Mode
                $('#loginbox').css('margin-top', '20%');
            }
            else if (window.orientation == 0 || window.orientation == 180) { //Portrait Mode
                $('#loginbox').css('margin-top', '40%');
            }
        }
    }
like image 198
Sayan Avatar answered Oct 14 '22 15:10

Sayan