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!

Christoph
  • 50,121
  • 21
  • 99
  • 128
Chad Emery
  • 23
  • 1
  • 5

1 Answers1

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");
Droid Chris
  • 3,455
  • 28
  • 31
  • 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. – Chad Emery Jan 20 '16 at 16:42
  • 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: http://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. – Droid Chris Jan 20 '16 at 17:26
  • This is not a correct answer to the question asked. – doctorram Apr 04 '22 at 16:15
  • How so @doctorram? It is doing what the question had asked? Do you want a javascript bridge answer? – Droid Chris Apr 04 '22 at 20:40
  • @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! – doctorram Apr 05 '22 at 21:28
  • @doctorram I am simply stating to parse a variable then make into HTML. Then you can load into a web view. If you want it to display the name, the HTML code I gave you would work. Javascript does not loadURL with a javascript command in it. – Droid Chris Nov 11 '22 at 22:17