Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhoneGap/Cordova Android get screen size after onDeviceReady

Android app returns invalid size (320x480) on onDeviceReady event, but after few seconds size becomes correct.

How can we get correct size at the very beginning? Or is there any event on which I can get correct size?

I'm using this function to get size:

function getWindowSizes() {
  var windowHeight = 0, windowWidth = 0;
  if (typeof (window.innerWidth) == 'number') {
      windowHeight = window.innerHeight;
      windowWidth = window.innerWidth;
      
  } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
      windowHeight = document.documentElement.clientHeight;
      windowWidth = document.documentElement.clientWidth;
      
  } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
     windowHeight = document.body.clientHeight;
     windowWidth = document.body.clientWidth;
  }
  return [windowWidth, windowHeight];
}

Viewport:

<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />

Using Cordova 2.5.0.

UPD:
As you can see on this screenshot application launches with smaller size. Even empty Cordova project does this. How can I fix this?

enter image description here

Thanks!

like image 303
dymv Avatar asked Mar 14 '13 09:03

dymv


2 Answers

Remove height, width and density properties on meta header index.

like image 191
Thierry Avatar answered Nov 07 '22 19:11

Thierry


Try to remove target-densitydpi in the header.

device dpi is based on physical pixels while css pixels are logic pixels, they are some times different from each other, if you force them to the same, you'll see screen stretched.

window.devicePixelRatio may give you result of the conversion.(On webkit)

Update:

This link provide some more details on how it works: https://petelepage.com/blog/2013/02/viewport-target-densitydpi-support-is-being-deprecated/

like image 26
Heng Cao Avatar answered Nov 07 '22 18:11

Heng Cao