SmurfXXX
SmurfXXX

Reputation: 121

Get webview resolution from device resolution

Is there a way to calculate the web resolution of an android device starting with its device resolution?

My Pixel 3a has a device resolution of 1080x2176 pixels but when I browse pages on chrome the resolution of rendered pages is 393x808.

I'm going to customize a web-to-app Android application and when the page finishes to load my javascript can't get the window height and width.

For now I can pass the device resolution as headers so I can use PHP and javascript on my site to detect the passed value.

Here is the code:

Map <String, String> extraHeaders = new HashMap<String, String>();
int screenHorigin  = Resources.getSystem().getDisplayMetrics().heightPixels;
int screenWorigin = Resources.getSystem().getDisplayMetrics().widthPixels;
double screenHD = (screenHorigin/2.819);
double screenWD = (screenWorigin/3.056);

int screenH = (int) screenHD;
int screenW = (int) screenWD;

extraHeaders.put("screen-h", Integer.toString(screenH));
extraHeaders.put("screen-w",Integer.toString(screenW));
mWebView.loadUrl(mUrl, extraHeaders);

I know that I can't get the webview resolution until it is created and rendered but is there a way to detect the BROWSER resolution instead of DEVICE resolution?

Thanks!

Upvotes: 0

Views: 448

Answers (1)

SmurfXXX
SmurfXXX

Reputation: 121

I solved myself by finding and using the density value of phone:

DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
int screenHorigin  = displayMetrics.heightPixels;
int screenWorigin = displayMetrics.widthPixels;
float density = Resources.getSystem().getDisplayMetrics().density;
double screenHDI = (screenHorigin/density);
double screenWDI = (screenWorigin/density);

double screenH = ceil(screenHDI);
double screenW = ceil(screenWDI);

I see that this is the usable resolution for an app so it's perfect for my case.

Upvotes: 1

Related Questions