1

I have local html file under my assets folder. I am loading this file in my WebView and I want to call a static Java method from my local web page.

This is what I want to do;

<div class="content">
<div class="welcome">
    <p>GeneralConstants.getWelcomeMessage()</p>
</div>

I want to call getWelcomeMessage() method from my local webpage.

4 Answers 4

2

You can use JAVA Script Interface for communication between Android function and your java script method.

JavaScript running inside the WebView can call out to code in your Activity. You can use this to have your JavaScript trigger actions like starting a new activity, or it can be used to fetch data from a database or ContentProvider. The API for this is very simple: just call the addJavascriptInterface method on your WebView. You pass an object whose methods you want to expose to JavaScript and the name to use when making calls.

Look at here a nice example for Android - Webview and here.

Sign up to request clarification or add additional context in comments.

Comments

0

Two way communication between JavaScript and your application is possible through WebView.addJavascriptInterface(). Check this example:

http://android-developers.blogspot.com/2008/09/using-webviews.html

Comments

0

Binding JavaScript code to Android code

http://android-journey.blogspot.com/2010/01/android-webview.html

Using JavaScript in WebView

Android WebView, Javascript and CSS

http://lexandera.com/tag/webview

Comments

0

You are approaching this from the wrong direction. You can not access java code from the HTML page.

However, your HTML page can define a JavaScript interface and your Java code can access those. So instead of HTML page fetching the welcome message from Java code, you have Java code providing the welcome message to the HTML page.

webView.loadUrl("javascript:setWelcomeMessage('Welcome!')");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.