Reputation: 970
I need to make some UI part in javascript for android application for that I'm writing some code in javascript and load it in android web view. Now, I want to call java class from javascript.
Is it possible to call java class from javascript?
Thanks in Advance!
Upvotes: 2
Views: 31449
Reputation: 4424
It's possible, but I wouldn't do it for your given use case :) (the UI could be done with the Android framework)
You can add what they call "Javascript Interfaces", which are basically links from Javascript to Java code. First, define a class to bind containing the methods you which to call, and add it to your WebView :
class HelloInterface
{
public void test(String name)
{
Log.i(TAG, "Hello " + name);
}
}
webview.addJavascriptInterface(new HelloInterface(), "HELLO");
Then, from your javascript code, you can just call
window.HELLO.test("Webview");
Upvotes: 4
Reputation: 7383
its actually reall easy (and cool) use:
webView.addJavascriptInterface(js, "pageObject");
js is a java object made in you activty (or somewhere where you init the webview), pageObject is the namespace identifier (variable) you can use with javaScript on your webpage.
Upvotes: 0
Reputation: 5179
Refer the following urls,
http://www.rgagnon.com/javadetails/java-0170.html
Post #6 of-> http://ubuntuforums.org/showthread.php?t=565819
Hope this helps..
Upvotes: 1