Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onLongClickListener does not work on WebView

I have the following struktur to implement an longclicklistener. It works if I click on a text on the webview which contains a html-link, so I know the structure is not completely wrong.

I removed this link now and the listener just doesn't listen to clicks anymore. Does anybody know this problem and have some advices?

    private View.OnLongClickListener mLongClickHandler = new View.OnLongClickListener()   {
    @Override
    public boolean onLongClick(View view) {
        ...
        return true;
    }
};

...

mywebview.setOnLongClickListener(mLongClickHandler);
like image 582
nob Avatar asked Jan 11 '11 15:01

nob


People also ask

How do you override a WebView?

If you want to override certain methods, you have to create a custom WebView class which extends WebView . Also, when you are inflating the WebView , make sure you are casting it to the correct type which is CustomWebView . CustomWebView webView = (CustomWebView) findViewById(R. id.


2 Answers

I tried now to clone the longclick action by myself. This works but only a few times. After a certain time, the onTouch-Event is not called anymore... Suggestions?

private Runnable copyTextAfterDelay=new Runnable() {
    public void run() {
        ...
    }
};

...

        myWebView.setOnTouchListener(new View.OnTouchListener() { 
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) { 
                    case MotionEvent.ACTION_DOWN:  
                        mTimerHandler.removeCallbacks(copyTextAfterDelay);
                        mTimerHandler.postDelayed(copyTextAfterDelay,1000);
                        break;
                    case MotionEvent.ACTION_UP: 
                        mTimerHandler.removeCallbacks(copyTextAfterDelay);
                        break;
                    case MotionEvent.ACTION_MOVE:
                        mTimerHandler.removeCallbacks(copyTextAfterDelay);
                        break;
                }
                return false;                  
            }
            });
like image 141
nob Avatar answered Nov 15 '22 11:11

nob


Override onTouch methode of your webview and return true for ACTION_DOWN events. Thereby you consume your down event.

  @Override
  public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) { 
         case MotionEvent.ACTION_DOWN:  
            return true;
      }
   }
like image 31
Mufazzal Avatar answered Nov 15 '22 10:11

Mufazzal