Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setOnClickListener not response on Android WebView

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 (:

like image 528
ZoharAdar Avatar asked Aug 30 '10 11:08

ZoharAdar


People also ask

Why my setOnClickListener is not working?

You need to put the setOnClickListener in one of the activity callbacks. In your onCreate() method, move the button there and then setOnClickListener() . Show activity on this post.

What does setOnClickListener do in Android?

setOnClickListener(this); means that you want to assign listener for your Button “on this instance” this instance represents OnClickListener and for this reason your class have to implement that interface. If you have more than one button click event, you can use switch case to identify which button is clicked.

What is setOnClickListener in Java?

In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.

What is the use of setOnClickListener?

One of the most usable methods in android is setOnClickListener method which helps us to link a listener with certain attributes. While invoking this method a callback function will run. One can also create a class for more than one listener, so this can lead you to code reusability.


2 Answers

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()?

like image 182
Murmel Avatar answered Sep 17 '22 08:09

Murmel


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;
        }
    });
like image 39
Konstantin Burov Avatar answered Sep 18 '22 08:09

Konstantin Burov