Search code examples
androidwebviewclick

Android Webview Click


I have a webview and I sent a URL. The page display is okay, but I cannot interact with the page displayed. I cannot click the buttons, I have just a front ... Do you have an idea of the problem?

My code :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web);
    WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.loadUrl("www.google.com");       
    myWebView.setWebViewClient(new WebViewClient());
}

Solution

  • You can add onclick to webview with the following method:

    webView.setOnTouchListener(new View.OnTouchListener() {
    
            public final static int FINGER_RELEASED = 0;
            public final static int FINGER_TOUCHED = 1;
            public final static int FINGER_DRAGGING = 2;
            public final static int FINGER_UNDEFINED = 3;
    
            private int fingerState = FINGER_RELEASED;
    
    
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
    
                switch (motionEvent.getAction()) {
    
                    case MotionEvent.ACTION_DOWN:
                        if (fingerState == FINGER_RELEASED) fingerState = FINGER_TOUCHED;
                        else fingerState = FINGER_UNDEFINED;
                        break;
    
                    case MotionEvent.ACTION_UP:
                        if(fingerState != FINGER_DRAGGING) {
                            fingerState = FINGER_RELEASED;
    
                            // Your onClick codes
                           Toast.makeText(WebActivity.this,"Click Detected",Toast.LENGTH_LONG).show();            
    
                        }
                        else if (fingerState == FINGER_DRAGGING) fingerState = FINGER_RELEASED;
                        else fingerState = FINGER_UNDEFINED;
                        break;
    
                    case MotionEvent.ACTION_MOVE:
                        if (fingerState == FINGER_TOUCHED || fingerState == FINGER_DRAGGING) fingerState = FINGER_DRAGGING;
                        else fingerState = FINGER_UNDEFINED;
                        break;
    
                    default:
                        fingerState = FINGER_UNDEFINED;
    
                }
    
                return false;
            }
        });
    

    If there is a button inside webview, the webpage that you are displaying inside the webview will handle that.You cannot get that callback.