2

I am trying to pass versionName string to webview and display the string on the page in my html. Here is what I have tried.

try {
    myWebView.getSettings().setJavaScriptEnabled(true);
    String versionName = getPackageManager()
                            .getPackageInfo("com.chad.flashupdate", 0)
                            .versionName;
    myWebView.loadUrl("javascript:init('" + versionName + "')");
} catch (NameNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Then I tried to load and display the string using this.

 $( document ).ready(function() { $("#MyEdit").html(versionName); });

In my html I put this.

<div id="MyEdit"><B>
 versionName will be displayed here!
</B></div>

When I try that nothing happens, meaning the div text never changes. So maybe the webview is not loading the string. Thanks for any help!

1 Answer 1

6

You should not need javascript for this. Try this instead:

String htmlString = "<div id='MyEdit'><b>VERSION NAME</b></div>";
webView.loadData(htmlString, "text/html; charset=utf-8", "UTF-8");
Sign up to request clarification or add additional context in comments.

6 Comments

One more question. When I load the webview it only shows the div. How can I load the normal html file in the assets and have that div change to the the htmlString? When I add your code it only shows that div on load.
That makes things a bit more complex. There are ways to load html from a file, but putting your version into would be more complex. Here is a link for getting the asset file as a string: stackoverflow.com/questions/16110002/read-assets-file-as-string. After that, I would look for a certain text character then replace that with the version you have.
This is not a correct answer to the question asked.
How so @doctorram? It is doing what the question had asked? Do you want a javascript bridge answer?
@DroidChris the question was how to pass a string parameter to HTML. Your answer initiates WebView from a string. Almost completely unrelated to the question!
|

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.