24 April, 2012

Syntax Highlighting with Blogger Engine 2

CraftyFella's already wrote some awesome hints about how to setup and use Alex Gorbatchev's SyntaxHighlighter javascript libray and I'm not used to beat dead horses but there is still one good hint he missed: THEMES.

It's really easy, just add the css file in your template and be happy my friend!

By the way, anyone knows how to setup this css theme to use different colors on odd and even lines?

23 April, 2012

Android issues with WebView.loadData and WebView.getContentHeight

If you tried the suggested code at Stack Overflow on Gingerbread you will see that it still doesn't work when using loadData or loadDataWithBaseURL. The proposed code only works when using the loadUrl - at least it didn't worked on my HTC Desire nor the 2.3.3 Emulator.

In the Andromedev blog you will see one possible solution using the android javascript bridge but I'm not still convinced that this is the best approach to take. Even if you do decide that it's the best approach you will face one bug already reported on Issue 12987 that affects any javascript bridged call on Gingerbread.

Right know I'm developing one pager fragment that uses one webView to display simple html texts and I had disabled the javascript support to try to keep the memory usage low. With the proposed solution I'll have to turn it on again and pray that my user don't use the gingerbread version ...

Well, the point is: IF you are using the WebView.loadUrl AND you are not on gingerbread you don't need to worry, you just need to write a listener - the onPageFinished and all height and width methods will work as expected.

However the code below still doesn't work! The java script bridge call the code in the JavaScriptInterface but the height is not there yet ...

/**
 * To use the webView embedded in another view (not full screen).
 */
public class EmbeddedWebView2 extends WebViewClient {

   private static final String P_MIMETYPE     = "text/html";
   private static final String P_ENCODING     = "utf-8";
   private static final String LOG_TAG        = "EmbeddedWebClientView2";
   private static final String JS_BRIDGE      = "JSB";
   private static final String JS_CONTENTSIZE = "javascript:window." + JS_BRIDGE + ".setContentSize(" + //
                                                    "document.getElementsByTagName('html')[0].scrollHeight," + //
                                                    "document.getElementsByTagName('html')[0].scrollWidth" + //
                                                    ");";

   @Override
   public void onPageFinished(WebView webView, String url) {

      super.onPageFinished(webView, url);

      WebSettings webSettings = webView.getSettings();
      webSettings.setJavaScriptEnabled(true);
      webView.addJavascriptInterface(new JavaScriptInterface(), JS_BRIDGE);
      webView.loadUrl(JS_CONTENTSIZE);
   }

   public static void loadData(WebView webView, String data) {

      webView.setWebViewClient(new EmbeddedWebView2());

      WebSettings webSettings = webView.getSettings();
      webSettings.setSavePassword(false);
      webSettings.setSaveFormData(false);
      webSettings.setJavaScriptEnabled(true);
      webSettings.setUseWideViewPort(false);

      final String dataHtml = "" + data + "";
      webView.loadData(dataHtml, P_MIMETYPE, P_ENCODING);
   }

   public static void loadDataWithBaseURL(WebView webView, String baseUrl, String data) {

      webView.setWebViewClient(new EmbeddedWebView2());

      WebSettings webSettings = webView.getSettings();
      webSettings.setSavePassword(false);
      webSettings.setSaveFormData(false);
      webSettings.setJavaScriptEnabled(true);
      webSettings.setUseWideViewPort(false);

      final String dataHtml = "" + data + "";
      webView.loadDataWithBaseURL(baseUrl, dataHtml, P_MIMETYPE, P_ENCODING, null);
   }

   /**
    * getHeight, getContentHeight and getMeasuredHeight are ONLY updated when
    * using loadUrl that's why we need this guy. 
    * 
    * TODO: wait a fix for ISSUE 12987 on gingerbread.
    */
   public class JavaScriptInterface {

      private int height, width;

      public void getContentSize(String sHeight, String sWidth) {

         if (sHeight != null) {
            width = Integer.parseInt(sHeight);
            Log.d(LOG_TAG, "Result from javascript, height = " + height);
         }
         if (sWidth != null) {
            width = Integer.parseInt(sWidth);
            Log.d(LOG_TAG, "Result from javascript, width = " + width);
         }
      }
   }
}

18 April, 2012