After a lot of research, I merged some answers that did not worked separately, and realised that together worked perfectly for me.
This disables double tap zoom in a WebView.
First, you need to set this in your WebView:
YourWebView.getSettings().setUseWideViewPort(false);
Then, you need to scale your WebView. But first need to get the scale value:
private int GetWebViewScale(){
int WebViewWidth = YourWebView.getWidth();
Double WebViewScale = new Double(WebViewWidth)/new Double(YourWebWidth); //Here, you must change YourWebWidth with your web width (in pixels)
WebViewScale = WebViewScale * 100d;
return WebViewScale.intValue();
}
Now you can get the scale value with:
YourWebView.setInitialScale(GetWebViewScale());
But, it will not work if you start the code too early, so you will put that code in WebViewClient's onLayoutChange override like this:
YourWebView.addOnLayoutChangeListener(new View.OnLayoutChangeListener(){
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
YourWebView.setInitialScale(getScale());
}
});
Attention: You will probably have thess issues: (Both happened to me)
- The WebView moves a little bit horizontally. (#1)
- The WebView is not scaling. (#2)
Fix issue #1:
- Just add some pixels in the "YourWebWidth" value. For example:
Before:
Double WebViewScale = new Double(WebViewWidth)/new Double(500); //(500 is my web's width)
After:
Double WebViewScale = new Double(WebViewWidth)/new Double(505); //(500+5) - (This will add some padding at the left and right of your WebView. 2,5px at the left side; 2,5 at the right side)
Fix issue #2
Probably your WebView's visibility is "gone", and when it is, it does not have width. So calling "GetWebViewScale" will not work.
You need to call it when its visibility is "visible" or "invisible".