0

I can detect a long click on my WebView using the following code:

webView.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Log.d("Debug","On Long Press Web View");
            return false;
        }
    });

This works fine when i long press on a link, but doesnt work when I long press in an area where no link is, i.e whitespace.

My first thought to address this was to use the GestureDetector:

public boolean dispatchTouchEvent(MotionEvent event) {              
    super.dispatchTouchEvent(event);
    return detector.onTouchEvent(event);
}

or

public OnTouchListener otl = new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (detector.onTouchEvent(event)){
            return true;
        }else{
            return false;
        }
    }
};

And then overide the onLongPress method of the GestureDetector:

public void onLongPress(MotionEvent e) {

}

Either of the above methods above work and the onLongPress method is executed when I long click on the web page but not on a link. The problem is when I now long click on a link both long press methods are called, firstly the GestureDetector onLongPress and secondlythe WebView onLongClick. Is there a way to just invoke only the WebView onLongClick() when i long click on a link.

Andy

Bear
  • 1,541
  • 3
  • 20
  • 32

3 Answers3

1

you should override webview.onTouchEvent() and then use postDelayed method(). i.e. postDelayed(mLongPressRunnable, mLongPressTimeout);

mLongPressRunnable is new java.lang.Runnable()

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
bigtree
  • 43
  • 5
0

You should try webView.setLongClickable(true);

Tim Post
  • 33,371
  • 15
  • 110
  • 174
  • 2
    While this theoretically may answer the question, it would be nice if you could provide some explanation as to why you think it would help. Please consider editing, or the community might remove this as a non-answer. – Tim Post Feb 14 '13 at 12:37
0

I only needed a simple solution here. No Runnable was needed. All I did was drop part of the question above into my gestureDetector class

@Override
public void onLongPress(MotionEvent e) {
    Toast.makeText(mContext, "LongPress", Toast.LENGTH_SHORT).show();
}

I did NOT set the WebView longClickable(). And I get a "LongPress" toast on whitespace, links, images, anywhere. I should add that my WebView is an extended WebView class with this internal gestureDetector class that extends SimpleOnGestureListener.

R Earle Harris
  • 985
  • 9
  • 17