Friday, 1 March 2013

Android - load and show html from a file into a TextView

There's a known bug in the Android WebView such that a WebView set with a transparent background colour does not appear with a transparent background on all devices. None of the workarounds are definitive and work for all devices. An alternative to loading html in a WebView is to load it in a TextView. There are limitations as to what HTML tags can be used (see here) but if you're only doing basic HTML tagging, this will work:

try
{
  InputStream inputStream = getResources().getAssets().open("myFile.html");

  String html = IOUtils.toString(inputStream);

  myTextView.setText(Html.fromHtml(html));
}
catch (IOException exception)
{
  myTextView.setText("Failed loading html.");
}

No comments: