Get click event from webpage in Android app

iam creates a sample webpage with a button .. this webpage is calling in android using webview.

now when i click the button on the webpage (this is the html button). I should be able to execute some codes in android ..

how to act?

public class web extends Activity { WebView mWebView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.webdisplay); mWebView = (WebView) findViewById(R.id.webview); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.loadUrl("http://localhost/test.html"); valid = new Button(ctx); valid.setOnClickListener(this); refuse = new Button(ctx); refuse.setOnClickListener(this); } 

}

+7
java javascript android
source share
1 answer

We can detect the following HTML elements according to the Android API docs.

 int ANCHOR_TYPE HitTestResult for hitting a HTML::a tag int EDIT_TEXT_TYPE HitTestResult for hitting an edit text area int EMAIL_TYPE HitTestResult for hitting an email address int GEO_TYPE HitTestResult for hitting a map address int IMAGE_ANCHOR_TYPE HitTestResult for hitting a HTML::a tag which contains HTML::img int IMAGE_TYPE HitTestResult for hitting an HTML::img tag int PHONE_TYPE HitTestResult for hitting a phone number int SRC_ANCHOR_TYPE HitTestResult for hitting a HTML::a tag with src=http int SRC_IMAGE_ANCHOR_TYPE HitTestResult for hitting a HTML::a tag with src=http + HTML::img int UNKNOWN_TYPE Default HitTestResult, where the target is unknown 

I think you can get all the events using the WebView function setOnTouchListener.

WebView has an inner class called HitTestResult . The HitTestResult class will help us find the HTML element that clicks when the user clicks on the WebView.

The HitTestResult class has only two methods.

  • getExtra (): returns a string. String has an HTML element that the user clicked.
  • getType (): returns an integer. It is used to determine which HTML element the user clicked.

You can do this:

 wv.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { WebView.HitTestResult hr = ((WebView)v).getHitTestResult(); Log.i(TAG, "getExtra = "+ hr.getExtra() + "\t\t Type=" + hr.getType()); return false; } }); 

Edited: See Perfect Answer: Detect click HTML button via javascript in Android WebView

+23
source share

All Articles