Rectangle 27 0

The WebView on most android 2.x devices does not generate more than one touch event in javascript and instead zooms the browser content - thus the "distortion".

In order to support those devices you can enhance your web container to generate those missing events - which we have done before with this "Polyfill" you may find on github.

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

android - Using Pinch in zoom in webview with a local html gets distor...

android html webview zoom pinch
Rectangle 27 0

Found a solution:

class MyWebView extends WebView { 

    public boolean onTouchEvent(MotionEvent event) {

            gd.onTouchEvent(event);

            // disable double tap zooming

        if(doubleTap)
            {
                doubleTap = false;
                return false;
            }

            return super.onTouchEvent(event);
        }


    GestureDetector.SimpleOnGestureListener sogl = new GestureDetector.SimpleOnGestureListener() { 




            public boolean onDoubleTap(MotionEvent e) {

                    showToast("Double tap");
                    doubleTap = true;

                    return false;
            }        
}

Android WebView use setWideViewPort, disable double-tap zoom but keep ...

android android-webview
Rectangle 27 0

webview.getsettings().setbuiltinzoomcontrols enables scaling of the webview as a whole (just like stretching an image), and does not communicate with the JavaScript code.

You will notice same thing happening if you open Google Maps on Android Web Browser instead of running the native Google Map app.

You can capture the pinch gesture with onTouchListener and set the zoom value of Map in JavaScript by webview.loadUrl("javascript: YOUR_ZOOM_IN_FUNCTION").

android - Using Pinch in zoom in webview with a local html gets distor...

android html webview zoom pinch
Rectangle 27 0

check if this work - put a if condition checking if single finger touch is there then only do the swipe. change "event" variable to your local variable.

if(event.getPointerCount() == 1)
  {
    switch (action) {
     case (MotionEvent.ACTION_DOWN):
          swipeOriginX = event.getX();
          swipeOriginY = event.getY();
           break;

    case (MotionEvent.ACTION_UP):

    int quarterWidth = (int) (screenWidth * 0.3);
    float diffX = swipeOriginX - event.getX();
    float diffY = swipeOriginY - event.getY();
    float absDiffX = Math.abs(diffX);
    float absDiffY = Math.abs(diffY);

    if ((diffX > quarterWidth) && (absDiffX > absDiffY)) {
        x = 1;
        try {
            navigator.goToNextChapter(index);
            x = 0;
        } catch (Exception e) {
            x = 1;
            errorMessage(getString(R.string.error_cannotTurnPage));
        }
    } else if ((diffX < -quarterWidth) && (absDiffX > absDiffY)) {
        x = 1;
        try {
            navigator.goToPrevChapter(index);
            x = 0;
        } catch (Exception e) {
            x = 1;
            errorMessage(getString(R.string.error_cannotTurnPage));
        }


    break;    
    }
  }
}
else if(event.getPointerCount()==2)
 {
   // code for pinch zoom
 }

it's not working.. the pinch zoom that I am using the built in codes for the WebView. I just enabled the pinch zoom function in the settings of my WebView.

android - How can I use pinch zoom without calling my swipe function i...

android webview swipe gesture pinchzoom
Rectangle 27 0

The GestureDetector with "one(first) finger" solution does not work reliably. The web view is still sometimes zoomed on Galaxy S3 Android 4.0.4. An additional WebViewClient like this can be used to restore the zoom scale when the view is zoomed:

public class NoZoomedWebViewClient extends WebViewClient {
    private static final String LOG_TAG = "NoZoomedWebViewClient";
    private static final long STABLE_SCALE_CALCULATION_DURATION = 2 * 1000;

    private long   stableScaleCalculationStart;
    private String stableScale;  // Avoid comparing floats
    private long   restoringScaleStart;

    NoZoomedWebViewClient() {
        stableScaleCalculationStart = System.currentTimeMillis();
    }

    @Override
    public void onScaleChanged(final WebView view, float oldScale, float newScale) {
        Log.d(LOG_TAG, "onScaleChanged: " + oldScale + " -> " + newScale);

        long now = System.currentTimeMillis();
        boolean calculating = (now - stableScaleCalculationStart) < STABLE_SCALE_CALCULATION_DURATION;
        if (calculating) {
            stableScale = "" + newScale;
        } else if (!stableScale.equals("" + newScale)) {
            boolean zooming = (now - restoringScaleStart) < STABLE_SCALE_CALCULATION_DURATION;
            if (!zooming) {
                Log.d(LOG_TAG, "Zoom out to stableScale: " + stableScale);
                restoringScaleStart = now;
                view.zoomOut();

                // Just to make sure, do it one more time
                view.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        view.zoomOut();
                    }
                }, STABLE_SCALE_CALCULATION_DURATION);
            }
        }
    }
}

Android WebView use setWideViewPort, disable double-tap zoom but keep ...

android android-webview
Rectangle 27 0

GestureDetector.SimpleOnGestureListener sogl = new GestureDetector.SimpleOnGestureListener() {
    public boolean onDoubleTap(MotionEvent e) {
        showToast("Double tap");
        return true; //instead of false
    }
}

Thanks for your input, but I have expiremented with the return value and it does not seem to make any difference. Strange.

Android WebView use setWideViewPort, disable double-tap zoom but keep ...

android android-webview
Rectangle 27 0

public class HelpWebView extends WebView {

    private GestureDetector gestureDetector;
    private AtomicBoolean mPreventAction = new AtomicBoolean(false);
    private AtomicLong mPreventActionTime = new AtomicLong(0);

    public HelpWebView(Context context) {
        super(context);
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    public HelpWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    public HelpWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    public HelpWebView(Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
        super(context, attrs, defStyle, privateBrowsing);
        gestureDetector = new GestureDetector(context, new GestureListener());
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int index = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        int pointId = event.getPointerId(index);

        // just use one(first) finger, prevent double tap with two and more fingers
        if (pointId == 0){
            gestureDetector.onTouchEvent(event);

            if (mPreventAction.get()){
                if (System.currentTimeMillis() - mPreventActionTime.get() > ViewConfiguration.getDoubleTapTimeout()){
                    mPreventAction.set(false);
                } else {
                    return true;
                }
            }

            return super.onTouchEvent(event);
        } else {
            return true;
        }
    }

    private class GestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            mPreventAction.set(true);
            mPreventActionTime.set(System.currentTimeMillis());
            return true;
        }
        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            mPreventAction.set(true);
            mPreventActionTime.set(System.currentTimeMillis());
            return true;
        }
    }
}

Android WebView use setWideViewPort, disable double-tap zoom but keep ...

android android-webview