Tuesday, November 23, 2010

Android WebView and WebViewClient example

private static void initWebkit(WebView web, WidgetActivity activity) {

  final String mimetype = "text/html";
  final String encoding = "UTF-8";
  String htmldata = "<html><body>boo</body></html>";

  {
    String data = ResourceUtils.loadResToString(R.raw.content, activity);
    if (data != null) htmldata = data;
  }

  web.loadData(htmldata,
               mimetype,
               encoding); 
 
web.setWebViewClient(new WebViewClient() { 
 
@Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
      Log.i(Global.TAG, "Panel2Builder... webview URL:" + url);

      if (url.contains("clock")) {
        String html = "<html><body>Date:" + new Date().toString() + "</body></html>";
        view.loadData(html, mimetype, encoding);
        Log.i(Global.TAG, "Panel2Builder intercepting webview url click...");
        return true;
      }
      else {
        Log.i(Global.TAG, "Panel2Builder not processing webview url click...");
        return false;
      }

    }
  });
 
}

1 comment:

AnOutsider said...

wats the purpose of this line Log.i(Global.TAG, "Panel2Builder... webview URL:" + url);