I am showing a webpage in an Android WebView. The page needs to be scaled to fit the available width of the WebView.
HTML (width 550 is just an example, that could change):
<html>
<head>
<meta name="viewport" content="width=550">
...
Java:
mWebView = (WebView)findViewById(R.id.webView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.setVerticalScrollBarEnabled(false);
mWebView.requestFocus(View.FOCUS_DOWN);
mWebView.getSettings().setLoadWithOverviewMode(true); // required for scaling
mWebView.getSettings().setUseWideViewPort(true); // required for scaling
mWebView.loadUrl(someUrl);
This works fine, except one problem: double-tab triggers a switch between overview mode and default scale.
Is there any way to disable this functionality?
Btw: I cannot calculate the scale in the code, because the loaded pages might have different sizes, which I do not know beforehand. I also already checked the source code of WebView, but do not see anything, that I could override or change.
You could write your own WebView implementation implemting OnGestureListener:
Inside declare a GestureDetector:
Implement a initWebView() method and call it in the constructor like this:
Now implement the methods from OnGestureListener and return true on double tap events:
Finally Override OnTouchEvent of your webview and let it pass through events to the gesture detector like this:
This works pretty good generally but there is 1 basic flaw to this solution: Some Events that are not identified as DoubleTap events could cause the Zoom of your webview.
I´m still working on a solution for that and will post my progress here:
WebView getting rid of double tap zoom.