Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the click event from webpage in my Android application

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);
    }
}
like image 700
Rockin Avatar asked May 06 '11 06:05

Rockin


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 to set Button click event in android?

To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

How to set click listener in android?

If you have more than one button click event, you can use switch case to identify which button is clicked. Link the button from the XML by calling findViewById() method and set the onClick listener by using setOnClickListener() method. setOnClickListener takes an OnClickListener object as the parameter.


1 Answers

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() + "\t\t Type=" + hr.getType());
            return false;
        }
    });

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

like image 107
Kartik Domadiya Avatar answered Sep 25 '22 22:09

Kartik Domadiya