6

I am using this code and it works exactly as I want. But I have to implemented another feature on double tap and would like to disable the double tap zooming (but keeping the pinch zoom feature).

webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true); 
webView.setInitialScale(1);

Tried to manually calculate the scale, with no luck (which feels complicated for this). Disable Double Tap Zoom/Unzoom on a webview

Android Webview - Webpage should fit the device screen

Is there a way to use the setUseWideViewPort and Zoomcontrolls but only disable or override the double tap zoom?

Community
  • 1
  • 1
jannej
  • 864
  • 14
  • 26
  • Could you add the code where you override the doubletap event? – Ferdau Apr 26 '12 at 09:15
  • I am extending WebView with MyWebView. public MyWebView(FriarBook context) { super(context); gd = new GestureDetector(context, sogl); } GestureDetector.SimpleOnGestureListener sogl = new GestureDetector.SimpleOnGestureListener() { public boolean onDoubleTap(MotionEvent e) { showToast("Double tap"); return false; } ...sort of I am a newbie in Android and Java...the onDoubleTap fires but is not overriding double tap zoom. – jannej Apr 26 '12 at 09:46

4 Answers4

12

best solution, just extend your WebView from MyWebView

 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;
        }
    }
}
iscariot
  • 434
  • 7
  • 14
3

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;
            }        
}
jannej
  • 864
  • 14
  • 26
1

Sorry, I don't have time to test this but try:

GestureDetector.SimpleOnGestureListener sogl = new GestureDetector.SimpleOnGestureListener() {
    public boolean onDoubleTap(MotionEvent e) {
        showToast("Double tap");
        return true; //instead of false
    }
}
Ferdau
  • 1,524
  • 1
  • 12
  • 21
  • Thanks for your input, but I have expiremented with the return value and it does not seem to make any difference. Strange. – jannej Apr 26 '12 at 11:14
1

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);
            }
        }
    }
}
Ngoc Dao
  • 1,501
  • 3
  • 18
  • 27