Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phonegap android get the actual screen size

I know there are a lot of questions similar to my title but I couldn't find the answer that I want.

In my app, I am using window.innerWidth and window.innerHeight to get the screen size, but these two values return different size. For example when I run my app on Note 3, I get 360x615 while it should be 1080x1920.

The following code in Java gives me what I want:

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;

So is there any way in Phonegap to get the same screen size value as Java?

Update: when I test the same code on Note 2 with Jelly Beans I get the correct size 720x1230

like image 775
HaiderSahib Avatar asked Sep 30 '14 19:09

HaiderSahib


People also ask

How can I get mobile screen width and height in android?

Display display = getWindowManager(). getDefaultDisplay(); Point size = new Point(); display. getSize(size); int width = size. x; int height = size.


2 Answers

I am using this code hope it help you

var physicalScreenWidth = window.screen.width * window.devicePixelRatio;
var physicalScreenHeight = window.screen.height * window.devicePixelRatio;
like image 175
Sadegh Teimori Avatar answered Oct 01 '22 01:10

Sadegh Teimori


I am assuming you want to figure out the actual device pixels instead of the more commonly measured CSS pizels? You could read up on this here.

Basically, if you want to know actual device pixels you could use screen.width and screen.height. If you want to know CSS pixels you could use window.innerWidth and window.innerHeight.

The meta viewport tag may be ignored on certain Android devices, those are my experiences with Samsung Galaxy tab devices anyway.

like image 28
HischT Avatar answered Oct 01 '22 02:10

HischT