10

I wanted to know if there is any way that I can call a JavaScript function from native Android Activity.

I came across:

webView.loadUrl("javascript:hello()");

But it did not work for me.

Also, how will Android know in which html page does this JavaScript function reside?

1
  • plz also vote up to my answer ... :) Commented Aug 2, 2012 at 4:28

3 Answers 3

13

Android can only call the javascript method if an html page is currently loaded in webView

webView.loadUrl("javascript:hello()");

will call hello method writen in the html page only if the page containing this method is currently loaded in the webview control

first call

webview.loadUrl("Your html page url");

then call

webView.loadUrl("javascript:hello()");
Sign up to request clarification or add additional context in comments.

3 Comments

i have enabled everything as told in answer 1 and 2. But nothing works.
please check your html page on browser and checks is there any error on your html page javascript...
Solution is working without problems with arguments in javascript method. Nice one.
10

In case anyone has any issues with this like I did, you need to call your javascript function after the page has finished loading otherwise the javascript won't be called:

    webviewer.loadUrl("file:///android_asset/mypage.html");
    webviewer.setWebViewClient(new WebViewClient() {

        public void onPageFinished(WebView view, String url)
        {
            webviewer.loadUrl("javascript:hello()");
        }
    });

Comments

3

Before Using JavaScript, first you have to check Javascript is enabled or not in your application using "getJavaScriptEnabled () " (Its default FALSE). And loadUrl() method only used to load the current page only. Its does not do anything related to JavaScript.

EXAMPLE:

Setting up WebView

//initialization of webview

webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);

Loading HTML in WebView

webView.loadUrl("file:///android_asset/testhtml.html");

Setting WebChromeClient to webView

webView.setWebChromeClient(new MyJavaScriptChromeClient());

The MyJavaScriptChromeClient class Here we have to override onJsAlert() method to handle JavaScript Alert Function.

private class MyJavaScriptChromeClient extends WebChromeClient {
  @Override
  public boolean onJsAlert(WebView view, String url, String message,final JsResult result) {
//handle Alert event, here we are showing AlertDialog
    new AlertDialog.Builder(MainWebViewActivity.this)
       .setTitle("JavaScript Alert !")
       .setMessage(message)
       .setPositiveButton(android.R.string.ok,
           new AlertDialog.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                     // do your stuff
                     result.confirm();
               }
           }).setCancelable(false).create().show();
   return true;
  }
}

My Html File : testhtml.html :

<html>

  <body >

    <div onclick="alert('hello')"> Click Me !!  </div>

  </body>

</html>

How it Works ? When the text "Click Me !!" on the WebView is clicked, The android function onJsAlert(WebView view, String url, String message,final JsResult result) is called. The parameter to alert is copied to message parameter of onJsAlert function. And rest of the handling is done there. Here we are displaying a AlertDialog .

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.