Skip to content Skip to sidebar Skip to footer

Android: ZoomPicker Breaks OnTouchListener

I've got a webview which makes use of the built in zoom controls, as follows: wv = new WebView(this); wv.getSettings().setBuiltInZoomControls(true); Note: this activates two main

Solution 1:

So after running into the same problem myself, I tried to make sense of some of these answers to no avail. The problem at hand is that once the user invokes scroll (not pinch-zoom) (at least for me...), the WebView instance loses its OnTouchListener reference. How did I figure this out? Well...

Webview inherits dispatchTouchEvent() from View. dispatchTouchEvent calls onTouch() (which is the function not firing that should)

The reason why onTouch() wasn't getting called was, as I said before, that the WebView instance's OnTouchListener was getting set to null for some reason. This can be seen by putting a breakpoint in View's dispatchTouchEvent() method

So to solve this, we extend WebView like so:

import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;

public class TouchWebView extends WebView {

    WebTouchListener wtl;

    public TouchWebView(Context context) {
        super(context);
    }

    public TouchWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public TouchWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void resetTouchListener() {

        if(this.wtl == null) {
            this.wtl = new WebTouchListener();
        }
        this.setOnTouchListener(wtl);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {

        this.resetTouchListener();
        return super.dispatchTouchEvent(event);
    }
}

And then we implement our OnTouchListener:

import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.webkit.WebView;

public class WebTouchListener implements OnTouchListener {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        WebView.HitTestResult hr = ((TouchWebView)v).getHitTestResult();
        //Log.i(TAG, "getExtra = "+ hr.getExtra() + "\t\t Type=" + hr.getType());

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            System.out.println(hr.getExtra() + " " + hr.getType());
        }

        // TODO Auto-generated method stub
        return false;
    }


}

OnTouch will now fire even after zooming or scrolling


Solution 2:

I had the same problem, but managed to solve it by setting the listener again in my extended WebView class:

public void invalidate() {
    super.invalidate();
    setOnTouchListener(this);
}

Unfortunately, now I have the opposite problem in that the zoom controls (the plus/minus widget) does no longer receive touch events. There seems to be some exclusivity between having an OnTouchListener and zoom, at least in Android SDK level 8 (Froyo 2.2).

This sounds like a bug to me, but would love to find a workaround.


Solution 3:

The workaround is pretty simple. You have to create a custom class, that extends one of the ViewGroup subclasses like LinearLayout. Then override onInterceptTouchEvent - it will be called prior to ViewGroup child's onTouchEvent. It is not very elegant to put a webview control into your custom ViewGroup subclass, and then insert it into activity's view hierarchy - where it is desired, but - it works.

private class OwnedLayout extends LinearLayout
{
    protected OwnedLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    protected OwnedLayout(Context context)
    {
        super(context);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        //Put Your code here
        return super.onInterceptTouchEvent(ev);
    }


}

Solution 4:

Try

youWebView.getSettings().setBuiltInZoomControls(false);

It's work for me.


Solution 5:

A dirty solution is to cover an invisible view on top of your web view and detects touches there, and you have to distinguish if the touch is consumed, or return false to pass touches to underlying web view.


Post a Comment for "Android: ZoomPicker Breaks OnTouchListener"