ZoharAdar
ZoharAdar

Reputation: 474

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

Upvotes: 7

Views: 10844

Answers (3)

Gastón Saillén
Gastón Saillén

Reputation: 13129

For kotlin and compose, you first need to register onTouchEvent with performClick() in your view.

Example

AndroidView(factory = { context ->
            WebView(context).apply {
                layoutParams = ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )

                var clickCount = 0
                setOnTouchListener { v, event ->
                    when (event.action) {
                        MotionEvent.ACTION_DOWN -> {
                            v.performClick()
                        }
                    }
                    false
                }
                setOnClickListener {
                    clickCount++

                    if (clickCount >= 4 && BuildConfig.DEBUG) {
                        Toast.makeText(context, "Clicked 4 times !", Toast.LENGTH_SHORT).show()
                        clickCount = 0
                    }
                }
}

Upvotes: 0

Murmel
Murmel

Reputation: 5702

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

Upvotes: 7

Konstantin Burov
Konstantin Burov

Reputation: 69228

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;
        }
    });

Upvotes: 5

Related Questions