Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different width and height from DisplayMetrics and $(window).width() in WebView

Inside an Android app, I tried using the following way to obtain the screen width and height:

DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        final int screenWidth = metrics.widthPixels;
        final int screenHeight = metrics.heightPixels;

This reports 1080 and 1920 respectively for my phone, which matches the specs.

However, inside a webview, when I used:

var screenWidth = $(window).width();

This reports only 360.

From this what I understand, both are using pixel as unit, so why are the values different?

like image 473
paradite Avatar asked Mar 11 '17 13:03

paradite


1 Answers

Notice width is same in both cases but units are different.

E.g.

This is in Pixels metrics.widthPixels;

This one is in Dps 360 OR $(window).width();

Convert 360Dps to pixels

On xxhdpi device, 360dps = 1080px

Dp to pixels & vice versa

like image 81
PEHLAJ Avatar answered Oct 12 '22 04:10

PEHLAJ