user850234
user850234

Reputation: 3463

How to get the correct window width on orientation change for android devices both tablets and mobiles

I am trying to calculate the window width on orientation change for android devices using jquery function $(window).outerWidth(true);. This calculation gives correct width on orientation change for both iphone and ipad but not in android. If i initially load the page in landscape mode or portrait mode i am getting the correct width but once i change the orientation after loading the page i am getting the width for portrait mode as was in landscape mode and vice versa. Please suggest what is happening and how can i handle this issue so that i get the correct window width on orientation change in android device

Upvotes: 6

Views: 22893

Answers (7)

Bastien Robert
Bastien Robert

Reputation: 940

This is ugly, but it works. Mobile viewport height after orientation change

window.addEventListener('orientationchange', () => {
  const tmp = () => {
    this.onResize()
    window.removeEventListener('resize', tmp)
  }
  window.addEventListener('resize', tmp)
})

Upvotes: 0

silver1991
silver1991

Reputation: 1667

Instead of listening to orientationchange event , you can listen to window resize event , it will make sure that window.innerHeight/outerHeight properties got updated.

Upvotes: 1

Ifeanyi Chukwu
Ifeanyi Chukwu

Reputation: 3327

                            var isMobile = {
                                Android: function () {
                                    return navigator.userAgent.match(/Android/i);
                                },
                                BlackBerry: function () {
                                    return navigator.userAgent.match(/BlackBerry/i);
                                },
                                iOS: function () {
                                    return navigator.userAgent.match(/iPhone|iPad|iPod/i);
                                },
                                Opera: function () {
                                    return navigator.userAgent.match(/Opera Mini/i);
                                },
                                Windows: function () {
                                    return navigator.userAgent.match(/IEMobile/i);
                                },
                                webOS: function () {
                                    return navigator.userAgent.match(/webOS/i);
                                },
                                any: function () {
                                    return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
                                }
                            };


                            var tell_if_mobile;
                            var mobile_tablet;
                            if (isMobile.any()) {
                                tell_if_mobile = true;
                            } else {
                                tell_if_mobile = false;
                                var windowWidth = window.screen.width < window.outerWidth ?
                                                  window.screen.width : window.outerWidth;
                                tell_if_mobile = windowWidth < 800;
                                mobile_tablet = windowWidth >= 500 ? "Tablet/Phablet Device" : "Desktop View (Mobile)";
                            }

                            var mobile_type;
                            mobile_type = isMobile.Android() ? "Android Device" :
                                          isMobile.BlackBerry() ? "Blackberry Device" :
                                          isMobile.iOS() ? "iOS Device" :
                                          isMobile.Opera() ? "Opera Mobile/Mini" :
                                          isMobile.Windows() ? "IEMobile" :
                                          isMobile.webOS() ? "webOS device" :
                                          tell_if_mobile == true ? mobile_tablet :
                                          "Not a mobile device";
alert(mobile_type); //result

Upvotes: 1

Shivang Gupta
Shivang Gupta

Reputation: 3329

I was also stuck in this problem and find the best solution which will work on all the platforms.Below is the code of 'orientationchange' event.

function onOrientationChange(event)
{
    setTimeout(function(){
       'Enter you code here';
    },200);
}

Hope, it will help you and most of other too.. Cheers.

Upvotes: 7

CuriousGeorge
CuriousGeorge

Reputation: 7400

this post seems to have a solution that may be relevant to you:

http://forum.jquery.com/topic/orientationchange-event-returns-wrong-values-on-android

Upvotes: 1

d4rkpr1nc3
d4rkpr1nc3

Reputation: 1827

Try window.innerWidth, respectively, window.innerHeight; It is posible that android doesn't not about outerWidth and Height;

Upvotes: 0

slayton
slayton

Reputation: 20319

Why not just use the javascript screen object. you should be able to get the screen dimensions with :

screen.height;
screen.width;

Upvotes: 10

Related Questions