Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get onclick event on webview in android?

I want to know when the user clicks on webview but not on a hyperlink. On that click I want to show/hide a view of my activity that holds a webview. Any suggestion?

like image 931
Arslan Anwar Avatar asked Feb 25 '11 11:02

Arslan Anwar


People also ask

How do I get events on WebView?

This example demonstrates how do I get onClick event on webView in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do you communicate between WebView and native Android?

2.1 To receive data from webview ,we can create an interface, which will enable webview to connect the native layer and pass data. From native layer, create a class and replicate the following. While configuring web view, we need to set JavaScript interface as above JSBridge class.

What is the use of WebView in android?

The WebView class is an extension of Android's View class that allows you to display web pages as a part of your activity layout. It does not include any features of a fully developed web browser, such as navigation controls or an address bar. All that WebView does, by default, is show a web page.


2 Answers

I took a look at this and I found that a WebView doesn't seem to send click events to an OnClickListener. If anyone out there can prove me wrong or tell me why then I'd be interested to hear it.

What I did find is that a WebView will send touch events to an OnTouchListener. It does have its own onTouchEvent method but I only ever seemed to get MotionEvent.ACTION_MOVE using that method.

So given that we can get events on a registered touch event listener, the only problem that remains is how to circumvent whatever action you want to perform for a touch when the user clicks a URL.

This can be achieved with some fancy Handler footwork by sending a delayed message for the touch and then removing those touch messages if the touch was caused by the user clicking a URL.

Here's an example:

public class WebViewClicker extends Activity implements OnTouchListener, Handler.Callback {  private static final int CLICK_ON_WEBVIEW = 1; private static final int CLICK_ON_URL = 2;  private final Handler handler = new Handler(this);  private WebView webView; private WebViewClient client;  @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.web_view_clicker);          webView = (WebView)findViewById(R.id.web);     webView.setOnTouchListener(this);      client = new WebViewClient(){          @Override public boolean shouldOverrideUrlLoading(WebView view, String url) {              handler.sendEmptyMessage(CLICK_ON_URL);             return false;         }      };           webView.setWebViewClient(client);     webView.setVerticalScrollBarEnabled(false);     webView.loadUrl("http://www.example.com"); }  @Override public boolean onTouch(View v, MotionEvent event) {     if (v.getId() == R.id.web && event.getAction() == MotionEvent.ACTION_DOWN){         handler.sendEmptyMessageDelayed(CLICK_ON_WEBVIEW, 500);     }     return false; }  @Override public boolean handleMessage(Message msg) {     if (msg.what == CLICK_ON_URL){         handler.removeMessages(CLICK_ON_WEBVIEW);         return true;     }     if (msg.what == CLICK_ON_WEBVIEW){         Toast.makeText(this, "WebView clicked", Toast.LENGTH_SHORT).show();         return true;     }     return false; } } 

like image 58
Louth Avatar answered Oct 05 '22 19:10

Louth


You can implement an OnClickListener via OnTouchListener:

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                      }                     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;         }     }); 
like image 44
Hamidreza Hosseinkhani Avatar answered Oct 05 '22 19:10

Hamidreza Hosseinkhani