1

I am using btwebview to get text selection and handle it. The problem is when I use longPress with gestureDetector the default selection is also being launched, if I override onTouchEvent and return true, the problem is solved but I cannot click on any button or highlighted link on the webview, so I cannot access footnotes or videos inserted in the webview and shouldOverrideUrlLoading stops getting called.

  public void init(Context context) {
    System.out.println("BTWebview init");
    this.context = context;
    this.getSettings().setJavaScriptEnabled(true);
    gestureScanner = new GestureDetector(this);
    this.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            System.out.println("event "+event.toString() );
            return gestureScanner.onTouchEvent(event);
        }
    });

    setInitialScale(100);
    addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");

}

@Override
public boolean onTouchEvent(MotionEvent event) {
    System.out.print("on touch event "+ event.toString());
    return true;
}

@Override
public void onLongPress(MotionEvent e) {
    // TODO Auto-generated method stub
    // TODO Auto-generated method stub
            System.out.println("gesture listener onLongPress");

            mSelection = true;
            if(!this.isInSelectionMode()){
                System.out.println("onLongClick in selection mode");
                mSelection = true;
            }

            this.loadUrl("javascript:android.selection.longTouch();");
                mScrolling = true;
                System.out.println("onLongClick");

}
halfer
  • 19,824
  • 17
  • 99
  • 186
coder
  • 5,200
  • 3
  • 20
  • 45

1 Answers1

1

The reason that the webview has stopped responding to touches is because that functionality is implemented in the superclass' onTouchEvent.

So to get it to work again you will need to call super.onTouchEvent(event) somewhere in your onTouchEvent. Obviously just always calling it would get you back to where you started.

To achieve what you want to do you will need to call super.onTouchEvent only when you have not already detected that the event is a long press event. The simplest way to do this would be to store the pointer ID from the MotionEvent that is passed in onLongPress (you should be able to assume it will be the pointer at index 0 because a long press is by definition a single touch event).

Once you have this your onTouchEvent could look something like this

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getPointerId(0) != self.lastLongPressPointerId) {
        return super.onTouchEvent(event);
    }
    return true;
}

You might also need to watch for the ACTION_UP and ACTION_CANCEL events relating to the pointer and stop looking for it after that just incase the system decides to reuse the pointer ID.

Tristan Burnside
  • 2,546
  • 1
  • 16
  • 23
  • I'm sorry for the late reply, I have tried your suggestion but it didn't work. the problem when onTouchEvent is overridden like the one you posted the footnotes links and the button in the webview stop working. any other suggestion that I may try – coder Jan 14 '15 at 07:23
  • your logic is right but perhaps I have made some mistake, I will revise it again, but thank you for your idea. – coder Jan 14 '15 at 08:53
  • event.getPointerId(0) is always giving zero for all gestures, I think this is the problem. any other way to detect a certain event? – coder Jan 14 '15 at 09:14
  • ok I found the solution by setting a counter that counts down actions, if they are more than 2 then it is a long click and I return true else return super.onTouchEvent(event). – coder Jan 14 '15 at 11:34