7

I have Android LisView that was contain TextView to display the data in the list,

I add to change it to Webview, after doing that everything look good except the setOnClickListener that not responding anymore..

I have read about the Webview and found that setOnClickListener is not supported, instead setOnTouchListener is supported is there

a way to use the same functionality as setOnClickListener in Android WebView ?

Like this:

  myWebView.setOnClickListener(new OnClickListener(){ 
                          @Override 
                          public void onClick(View v) {

                          //do it ..

                          } 
                        }); 

Thanks (:

ZoharAdar
  • 474
  • 1
  • 7
  • 19

2 Answers2

7

I ended up with this solution:

public class ClickableWebView extends WebView {

    private static final int MAX_CLICK_DURATION = 200;
    private long startClickTime;

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

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

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

    @Override
    public boolean onTouchEvent(MotionEvent event) {

            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                startClickTime = Calendar.getInstance().getTimeInMillis();
                break;
            }
            case MotionEvent.ACTION_UP: {
                long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
                if(clickDuration < MAX_CLICK_DURATION) {
                    super.performClick();

                }
            }
        }
        return true;
    }

}

Remarks:

  • suppresses all click-events to anything inside the WebView (e.g.: hyperlinks)
  • simply add OnClickListener by adding an onClick in xml or in Java
  • does not interupt scrolling-gestures

Thanks to Stimsoni Answer to How to distinguish between move and click in onTouchEvent()?

Community
  • 1
  • 1
Murmel
  • 5,402
  • 47
  • 53
5

Why not use onTouch listener as you stated?

myWebView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            return false;
        }
    });
Konstantin Burov
  • 68,980
  • 16
  • 115
  • 93
  • Thanks Konstantin, maybe I miss something with the onTouch event I try it and it didn't work the way I need it. I have list that can be scroll, every item in the list open webpage when using the onTouch event while scrolling down the listView the onTouch event is fire,I can't scroll down, scroll down the list fire the onTouch event, I want to be able scrolling to the bottom of the list and then click. – ZoharAdar Sep 01 '10 at 08:46
  • You have to investigate the MotionEvent, base on the info you should be able to determine if user actually tries to scroll or just touched the screen. If that's scrollig just return true to let webview act as usual. – Konstantin Burov Sep 01 '10 at 08:56
  • Thanks Konstantin, Its look like MotionEvent.ACTION_UP do the work for the webview. The thing that is each item in the listview contain two webview's and two textview's,When using the onTouchEvent for single webview it work good but in this way I need to add onTouchEvent for each item,So I tried to use the onTouchEvent to myView.setOnTouchListener like this I can call it only one time for each row but doing that I am not getting any response. – ZoharAdar Sep 01 '10 at 10:18