Android WebView Limitations

I was trying to figure out how to display HTML help page for my Caltrain schedules application using WebView. I don’t want my application to require internet connection, so the HTML needs to ship with the application. Since help obviously needs to be localized, I tried to put the HTML into a string resource. This turned out to be tricky.

If string resources contain the quote (") or apostrophe (') characters, you need to either be able to wrap the whole string in either quote or apostrophe, or if you use both in the string then you need to espace with backslash (\). You will also need to replace less than (<) with &lt;. But even after this loadData() was giving me a blank page. By reducing my markup I found out I could not use any CSS styles. And even after that I still run into a problem where having hash (#) in the data meant nothing after the hash would show up (including the hash character). Using backslash to escape that or using a numeric entity &#35; instead did not help. At this point I gave up on localizing help, and went looking for ways to just include the HTML help file in the apk and load it using a file URL.

There are seemingly two locations to put “raw” resources in: /assets and /res/raw. It wasn’t clear to me how I could load HTML from /res/raw, so I went with /assets:

        browser.loadUrl("file:///android_asset/help.html");

Hopefully new SDK versions will make internationalizing HTML resources easier.

Similar Posts:

2 Comments

  1. sanat:

    just go through the androidmanifest.xml and two lines

    <uses-permission android:name=”android.permission.INTERNET”/>
    <uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION”/>

    then it ll show the web site

  2. Ranxerox:

    This is the code I used to parse from a raw resource…

    InputStream is = context.getResources().openRawResource(resourceID);
    InputStreamReader isr = new InputStreamReader(is);
    StringBuffer builder = new StringBuffer();
    char buffer[] = new char[1024];

    try {
    int chars;

    while ((chars = isr.read(buffer)) >= 0) {
    builder.append(buffer, 0, chars);
    }
    } catch (IOException e) {
    // do something useful
    }

    // then set the content from the string builder

    I rather suspect there is some simpler way, but then again it is Java so maybe not.