I’ve an android-app (Android SDK 10) with a WebView. On that WebView I have to use elements with a fixed position. Now I know, that there are Problems with fixed Elements, but with this Code in the HTML:
<meta name="viewport"
content="width=100%;
initial-scale=1;
maximum-scale=1;
minimum-scale=1;
user-scalable=no;">
And this for the Webview:
WebView mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.setVerticalScrollBarEnabled(true);
mWebView.loadUrl("path/to.html");
I am able to zoom when using the zoomcontrolls. Multitouch- and pinchzoom distorts the page however.
Is there a possibility to disable pich- and multitouchzoom but keep the zoom-controlls working?
With the suggestions of Vikalp Patel I’ve come to this solution:
CustomWebView mWebView = (CustomWebView) findViewById(R.id.webView1);
mWebView.loadUrl("path/to.html");
CustomWebView.java
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.webkit.WebView;
public class CustomWebView extends WebView {
/**
* Constructor
*/
public CustomWebView(Context context) {
super(context);
}
/**
* Constructor
*/
public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/*
* (non-Javadoc)
*
* @see android.webkit.WebView#onTouchEvent(android.view.MotionEvent)
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getPointerCount() > 1) {
this.getSettings().setSupportZoom(false);
this.getSettings().setBuiltInZoomControls(false);
} else {
this.getSettings().setSupportZoom(true);
this.getSettings().setBuiltInZoomControls(true);
}
return super.onTouchEvent(event);
}
}
Implementation in layout.xml
<package.path.CustomWebView
...
/>
Hope, that helps somebody.
I’ve looked at the source code for WebView and I concluded that there is no elegant way to accomplish what you are asking.
What I ended up doing was subclassing WebView and overriding
OnTouchEvent. In
OnTouchEventforACTION_DOWN, I check how many pointers there are usingMotionEvent.getPointerCount(). If there is more than one pointer, I callsetSupportZoom(false), otherwise I callsetSupportZoom(true). I then call thesuper.OnTouchEvent().This will effectively disable zooming when scrolling (thus disabling the zoom controls) and enable zooming when the user is about to pinch zoom. Not a nice way of doing it, but it has worked well for me so far.
Note that
getPointerCount()was introduced in 2.1 so you’ll have to do some extra stuff if you support 1.6.