Advertisement
n0tmE

android: load html content into WebView

Jul 22nd, 2011
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1.         // case 1: load static html content under assets folder
  2.         // you can have stylesheets or some external resources (e.g. image) as well
  3.         WebView webView = (WebView)findViewById(R.id.webView1);
  4.         webView.loadUrl("file:///android_asset/html/contentView.html");
  5.        
  6.         // case 2: load html file as template and replace with required content
  7.         // IOUtils is a utility to read input stream into String
  8.     try {
  9.         String htmlContent = IOUtils.readInputStream(getAssets().open("html/contentView.html"));
  10.             webView.loadDataWithBaseURL("file:///android_asset/html/", htmlContent.replace("{content}", "Your content"), "text/html", "utf-8", "about:blank");
  11.     } catch (IOException e) {
  12.         e.printStackTrace();
  13.     }
  14.     // need to manually set background color to transparent after load content
  15.     webView.setBackgroundColor(Color.TRANSPARENT);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement