2

I have already found many examples about how to call JavaScript from android. But it's not working for me. My target SDK is 17(android 4.2). This is how I am loading my html page from my activity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

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

    JavaScriptHandler jScriptHandler = new JavaScriptHandler(this); 
    WebChromeClient myWebChromeClient = new WebChromeClient();
    myWebView.setWebChromeClient(myWebChromeClient);
    myWebView.addJavascriptInterface(jScriptHandler, "MyHandler");

    myWebView.loadUrl("file:///android_asset/mywebpage.html");
    myWebView.loadUrl("javascript:myFunc()");


}

Here is the code for my JavaScriptHandler:

public class JavaScriptHandler {
//TabFragmentMap mapFragment;
Context context;
//Fragment fragment;

public JavaScriptHandler (Context c){
    this.context = c;
}

}

Here is the code for my html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>PhoneGap</title>
</head>
<body onload="myFunc()">
<h1 id="test1">Hello World</h1>
<input type="button" value="Say hello" onClick="moveMyself()" />
<div id="myDiv"></div>
<script type="text/javascript"> 


    function myFunc()
    {

       document.getElementById('test1').innerHTML = 'Good Morning';

    }

</script>
</body>
</html>
learner
  • 61
  • 7
  • did you had a chance to refer [this link.](http://stackoverflow.com/questions/20243686/android-callback-function-from-java-script-to-java) I hope this would be helpful for you. – chain Mar 18 '14 at 07:10

3 Answers3

2

Try this:

final WebView webview = (WebView)findViewById(R.id.browser);  
/* JavaScript must be enabled if you want it to work, obviously */  
webview.getSettings().setJavaScriptEnabled(true);  

/* WebViewClient must be set BEFORE calling loadUrl! */  
webview.setWebViewClient(new WebViewClient() {  
    @Override  
    public void onPageFinished(WebView view, String url)  
    {  
        webview.loadUrl("javascript:(function() { " +  
                "document.getElementsByTagName('body')[0].style.color = 'red'; " +  
                "})()");  
    }  
});  

webview.loadUrl("http://code.google.com/android");  
Android_coder
  • 9,953
  • 3
  • 17
  • 23
  • wow..this is working. Great effort. Thanks for your help. Now I have to call an already existing JavaScript function. Please give me some hint if you know anything. – learner Feb 08 '13 at 14:29
  • I could finally make it. Thanks a lot for your great help. Please see below my answer how to do it. – learner Feb 08 '13 at 14:34
2

It was actually the same thing that Tamilarasi has given me. If somebody wants to call an existing JavaScript function from the html, do the following:

myWebView.setWebViewClient(new WebViewClient() {
        public void onPageFinished(WebView view, String url){
            myWebView.loadUrl("javascript:myFunc()");
        }
});
myWebView.loadUrl("file:///android_asset/myHtml.html");
learner
  • 61
  • 7
-1

Try this links i hope this will be help to u:

http://android-er.blogspot.in/2011/10/call-javascript-inside-webview-from.html

Android 4.2.1, WebView and javascript interface breaks

Javascript interface not working with android 4.2

Community
  • 1
  • 1
Android_coder
  • 9,953
  • 3
  • 17
  • 23
  • Thanks for your answer. But these links is nothing different from what I am doing. It is not helping me :( – learner Feb 08 '13 at 12:41
  • I could call a android function from html JavaScript. But I can't do the other way: from android to JavaScript :( – learner Feb 08 '13 at 12:46
  • The 2nd and 3rd link that you have provided are about JavaScript -> Android interaction. I am successful with this. So, if a JavaScript function is executed, it is calling an Android function properly. But I need to pass data to html page via the JavaScript function. I am stuck with this issue. – learner Feb 08 '13 at 12:52
  • will you need like your enter the text into textbox the textbox text is diplay the javascript dialog box when you click the button – Android_coder Feb 08 '13 at 13:05
  • If you see my code, I want to call the JavaScript function named myFunc() inside my activity's onCreate(). But it is not being called :(. You can see similar example at the following link:http://blog.objectgraph.com/index.php/2012/03/16/android-development-javascript-bridge-example-fully-explained/ – learner Feb 08 '13 at 13:11
  • Probably I could not make my problem clear. The youtube example is about a button inside a html calling a JavaScript function. But I need to call it from android side. – learner Feb 08 '13 at 13:24