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.

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

4 Comments

Could you post some sample code for the first method?
Thanks I'll try the code if it works I'll mark it correct. If not I will ask more questions :)
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. 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.
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);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.