Calling Java method in Android from JavaScript using webview -


i'm trying access java method helloworld(); using javascriptinterface in android, page i'm viewing using webview

i'm little new this, , don't know why i'm not getting value html page

this android code i'm using:

webview web;  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     web = (webview) findviewbyid(r.id.webview01);     javascriptinterface jsinterface = new javascriptinterface(this);     web.getsettings().setjavascriptenabled(true);     web.addjavascriptinterface(jsinterface, "jsinterface");     web.getsettings().setusewideviewport(true);     web.getsettings().setloadwithoverviewmode(true);     web.setwebviewclient(new mywebclient());     web.getsettings().setjavascriptenabled(true);     web.loadurl("http://exampleweb:10110/exweb/deposits.jsp"); }  public class mywebclient extends webviewclient {     @override     public void onpagestarted(webview view, string url, bitmap favicon) {         // todo auto-generated method stub         super.onpagestarted(view, url, favicon);     }      @override     public boolean shouldoverrideurlloading(webview view, string url) {         // todo auto-generated method stub          view.loadurl(url);         return true;      } } public class javascriptinterface {     private activity activity;      public javascriptinterface(activity activity) {         this.activity = activity;     }     @javascriptinterface     public string helloworld(){         return("hello world");     } } 

}

this javascript code i've been using (on page i'm viewing webview)

html button

<td> <button type="button" class="btn btn-xs btn-primary connecttoandroid">test android</button></td> 

javascript

$(".connecttoandroid").on('click', function () { var val = window.jsinterface.helloworld(); alert(val); }); 

does know i'm doing wrong here? want alert "hello world" android in html page.

any pointers appreciated!

try adding line of code before loading url , make sure reference jquery library in html.

web.setwebchromeclient(new webchromeclient(this)); 

Comments