2

Imagine an HTML template that looks like this:

<html>
<head>
    <style type="text/css">
        body {
            color: white;
        }
    </style>
</head>
<body>
The changes include:
<br/><br/>
%1$s
<br/><br/>
Would you like to update now?
</body>
</html>

I know I can load the contents into a WebView with webView.loadUrl("file:///android_asset/html/upgrade.html"). But I also want to replace the $1 at runtime, just as if I were loading it from an XML file using String.format().

Is it possible to load the file contents into a String first so I can run String.format() on it and then use webView.loadData() instead?

Christopher Pickslay
  • 17,523
  • 6
  • 79
  • 92

3 Answers3

5

I came up with a solution based on this answer to a similar question:

String prompt = "";
AssetManager assetManager = getApplicationContext().getResources().getAssets();
try {
    InputStream inputStream = assetManager.open("html/upgrade-alert.html");
    byte[] b = new byte[inputStream.available()];
    inputStream.read(b);
    prompt = String.format(new String(b),changes);
    inputStream.close();
} catch (IOException e) {
    Log.e(LOGTAG, "Couldn't open upgrade-alert.html", e);
}

WebView html = new WebView(this);
html.loadData(prompt,"text/html","utf-8");
Community
  • 1
  • 1
Christopher Pickslay
  • 17,523
  • 6
  • 79
  • 92
1

just write whole html in some string replace one string that you want to replace and then load using loadData.

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/WebView1.html

http://sriram-iyengar.blogspot.com/2011/05/android-webview-loaddata-and-encoding.html

something like this

ud_an
  • 4,939
  • 4
  • 27
  • 43
  • I don't want to stuff all of that HTML into a string in code--I'd prefer to keep my text and HTML in resource files. I tried putting it in a strings.xml, but you have to escape all the brackets and quotes and it's a mess. – Christopher Pickslay Aug 31 '11 at 05:11
0

Well you could use HTMLCleaner to get the HTML from your WebView as a DOM and change it via TagNodes. An example how to use HTMLCLeaner to get a node an give out a certain attribute of it. You should do this in an AsyncTask of course, so that it doesn't block the ui.

public static String snapFromCleanedHTMLWithXPath(String stringURL, String xPath, String attrToStrip) {
    String snap = "";

    // create an instance of HtmlCleaner
    HtmlCleaner cleaner = new HtmlCleaner();

    // take default cleaner properties
    CleanerProperties props = cleaner.getProperties();

    props.setAllowHtmlInsideAttributes(true);
    props.setAllowMultiWordAttributes(true);
    props.setRecognizeUnicodeChars(true);
    props.setOmitComments(true);

    // open a connection to the desired URL
    URL url;
    try {
        url = new URL(stringURL);
        URLConnection conn = url.openConnection();

        // use the cleaner to "clean" the HTML and return it as a TagNode object
        TagNode root = cleaner.clean(new InputStreamReader(conn.getInputStream()));

        Object[] foundNodes = root.evaluateXPath(xPath);

        if (foundNodes.length > 0) {
            // casted to a TagNode
            TagNode foundNode = (TagNode) foundNodes[0];
            snap = foundNode.getAttributeByName(attrToStrip);
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (XPatherException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    cleaner = null;
    props = null;
    url = null;
    return snap;
}

HTMLCLeaner-Website

einschnaehkeee
  • 1,858
  • 2
  • 17
  • 20
  • Thanks @einschnaehkeee. This is an interesting approach, though it feels a bit complex for my needs. Basically I just want to be able to read the contents of the file into a string, so I can do string replacement on it. – Christopher Pickslay Aug 31 '11 at 17:54
  • Hmm perhaps you can achieve what you want with this approach: http://lexandera.com/2009/01/injecting-javascript-into-a-webview/ I hope this helps.^^ – einschnaehkeee Aug 31 '11 at 18:25