I have a basic web page that responds correctly to screen size and orientation changes (using CSS media queries) when displayed in Chrome on an Android tablet (Nexus 7). When I display the same page in a WebView, the media queries based on screen width do not work. Here's how I'm setting up the WebView:
WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.loadUrl("http://10.0.1.8:8080/cbc/test");
The WebView correctly detects orientation changes, based on the behavior of the following media queries:
@media only screen and (orientation: portrait) {
.landscape { display: none; }
}
@media only screen and (orientation: landscape) {
.portrait { display: none; }
}
Media queries using screen dimensions do not work correctly, because, according to the following JavaScript, the screen width and height remain constant through orientation changes:
$("#dimensions").empty();
$("#dimensions").append($("<div>Width: " + screen.width + "</div>"));
$("#dimensions").append($("<div>Height: " + screen.height + "</div>"));
$("#dimensions").append($("<div>Depth: " + screen.pixelDepth + "</div>"));
I trigger the previous code with an "orientationchange" event. When running in Chrome on Android, the width and height values are displayed correctly, taking device orientation into account. When running inside the WebView, the width and height remain constant, regardless of orientation.
Is there some configuration I need to apply to the WebView to get the expected behavior?