Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
175
rated 0 times [  181] [ 6]  / answers: 1 / hits: 28803  / 13 Years ago, fri, may 6, 2011, 12:00:00

I'm creating a sample webpage with button..this webpage am calling in Android using webview.



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



How to proceed?



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

More From » java

 Answers
44

We can detect following HTML elements as per Android API Document.



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 will be able to get all events using WebView's setOnTouchListener function.



WebView has inner class named HitTestResult.
HitTestResult class will help us to find the HTML element which press when user click on WebView.



HitTestResult class has only two method.




  1. getExtra() : It return String. String has HTML element which is clicked by user

  2. getType() : It return integer. It is used to identify which HTML element is clicked by user.



You can do like :



wv.setOnTouchListener(new View.OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {
WebView.HitTestResult hr = ((WebView)v).getHitTestResult();

Log.i(TAG, getExtra = + hr.getExtra() + tt Type= + hr.getType());
return false;
}
});


Edited :
Refer for perfect answer :
Detect click on HTML button through javascript in Android WebView


[#92377] Wednesday, May 4, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zackeryzainv

Total Points: 61
Total Questions: 102
Total Answers: 99

Location: Andorra
Member since Sat, May 27, 2023
1 Year ago
;