Change HTML in WebView programmatically

I am loading an html resource page in a WebView using

webMain.loadUrl("file:///android_asset/record.html"); 

which works fine, but inside html there are several places where I would like to use information from the application. For example, HTML may contain text that reads "[Custom]". Is there a way to replace this word with information passed from the application?

+4
source share
5 answers

Actually, I don’t understand why the size of the record.html file will affect the preservation of the code. Read the html line (using the Java reader class or something else) from the html file to the asset, use the replaceAll function with Regex to replace all [Custom] in the html file. How long html should not affect how you maintain the code. It is rather a performance issue, or the line is really very long, which exceeds the java line limit.

some code i used before:

 InputStream is = getApplicationContext().getAssets().open("details/product_jsmodify.html"); Reader r = new InputStreamReader(is); String details = Utils.readertoString(r); details = details.replace("%product_name%",productName ); 

Utils is my class that does the conversion to string. I do not use Regex here, as I am only replacing the word this time. Then I load the string as Cata. I think this is pretty clean.

+4
source

This is an old and already accepted question, however I am sure that the problem can be solved more elegantly using javascript.

Store the html file in the folder with your assets and surround the text you want to replace with div elements with unique identifiers.

 <html> <head> ... <head> <body> Static text <div id="replace1">replace me</div> <div id="replace2">replace me too</div> More static text ... </body> </html> 

Now create a javascript function that will replace the innerHtml div with id:

  function replace(id, newContent) { document.getElementById(id).innerHTML = newContent; } 

This function will be best placed directly in the html file, update the <head> section to look like this:

 <head> ... <script type="text/javascript"> function replace(id, newContent) { document.getElementById(id).innerHTML = newContent; } </script> </head> 

Now we need to call the javascript function from the Android api web application:

 WebView helpView = (WebView)findViewById(R.id.helpView); helpView.getSettings().setJavaScriptEnabled(true); helpView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); view.loadUrl("javascript:replace('replace1', 'new content 1')"); view.loadUrl("javascript:replace('replace2', 'new content 2')"); } }); helpView.loadUrl("file:///android_asset/help.html"); 

Using this, you will avoid reading potentially large data into memory and unnecessarily perform expensive operations.

+11
source

Yes, you can do this by loading your page into a String, and then loading that string into your WebView.

For instance:

 String summary = "<html><body>You scored <b>192</b> points.</body></html>"; webview.loadData(summary, "text/html", null); 

Taken from here

+2
source

This worked for me, with html along with text and images.

  InputStream is = getAssets().open(html_name); int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); String str = new String(buffer); str = str.replace("InitialTextToBeReplaced", "TextAfterReplacement"); //Now instead of webview.loadURL(""), I needed to do something like - webView.loadDataWithBaseURL("file:///android_asset/", str, "text/html", "UTF-8",null); 
+1
source

It worked for me.

 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Payment Demo</title> </head> <body> <div> <input type="text" id="uname " name="uname " value=""> <input type="text" id="pass" name="pass" value=""> </div> </body> </html> 

This is java code.

 WebView wb; wb = (WebView) findViewById(R.id.webView1); wb.loadUrl("file:///android_asset/web1.html"); wb.getSettings().setJavaScriptEnabled(true); wb.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView web, String url) { // TODO Auto-generated method stub String uname = " email@mail.com "; String pass = "******"; view.loadUrl("javascript:(function(){document.getElementById('uname').value = '"+uname+"';})()"); view.loadUrl("javascript:(function(){document.getElementById('pass').value = '"+pass+"';})()"); } }); 
+1
source

Source: https://habr.com/ru/post/1391992/


All Articles