0

Is it possible to detect button click on web page which loaded in an Android WebView? I am trying to display an image when particular button is clicked. Is there some way to achieve this?

kenny_k
  • 3,831
  • 5
  • 30
  • 41
Stephen Balaji
  • 195
  • 1
  • 2
  • 11

1 Answers1

-1

You can use JavascriptInterface annotation to interact with any web page in webview.

Define a method annotated with JavascriptInterface

public class WebAppInterface { Context mContext;

// Instantiate the interface and set the context
WebAppInterface(Context c) {
    mContext = c;
}

// Show a toast from the web page
@JavascriptInterface
public void showToast(String toast) {
    Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}

}

and add instance of this class to webview

webView.addJavascriptInterface(new WebAppInterface(this), "AndroidInterface");

and call method from javascript in web page.

// this is a javascript code

AndroidInterface.showToast('Hello');