0

I have an application that is displaying a static HTML file that is included with the application.

The HTML file has certain strings that I would like to change based on the value of a spinner.

The strings are formatted as follows:

{{string1}} {{string2}} ... {{stringx}}

Where a string may occur multiple times.

Is it possible to do a find and replace on the web-view as the result of a spinner change using a listener?

How would I go about doing this?

Would it be possible to somehow trigger java-script on in the html to make this happen?

As a sub note I could change the format of the HTML file if it would help me accomplish my goals.

earnshae
  • 169
  • 13

1 Answers1

0

There are two possibilities.

First: load html as string using method loadDataWithBaseURL and perform string operations.

Second: adding addJavascriptInterface to webview.link

Example for First:

InputStream is = getApplicationContext().getAssets().open(“my.html");
Reader r = new InputStreamReader(is);
int size = is.available(); 
char[] buffer = new char[size]; 
r.read(buffer); 
r.close(); 
String data = new String(buffer);

data = data.replace(“%stringData%”,replaceData );

webview.loadData(data, "text/html", null);
Community
  • 1
  • 1
Dharmaraj
  • 1,256
  • 13
  • 12
  • Could you post some sample code for the first method? – earnshae Apr 18 '16 at 19:40
  • Thanks I'll try the code if it works I'll mark it correct. If not I will ask more questions :) – earnshae Apr 18 '16 at 19:58
  • Okay your method is correct and everything seems to work, but I had one small problem with Utils.readertoString(r); Did you mean apache IOUtils? Or is Utils a custom library? I ended up using Guava since I had it in the project as seen in the link. http://www.baeldung.com/java-convert-reader-to-string Make an adjustment to the code so that anyone referring to this answer will not be confused and I will mark it as correct. – earnshae Apr 18 '16 at 20:37
  • its my common method to convert: replace String data=Utils.readertoString(r); with int size = is.available(); char[] buffer = new char[size]; r.read(buffer); r.close(); String str = new String(buffer); – Dharmaraj Apr 18 '16 at 20:56