Angular Multiple ng-transclude

Quick post that may save someone a lot of time and frustration. Transclusion is a great feature in Angular (see the documentation). The ability to use multiple ng-transclude bits in your template makes it even greater. However, it took me a long time to get the simplest example to work. The problem was that each occurrence of ng-transclude in my template, even though assigned to a specific slot, did end up including everything from the HTML and not just the specified slot.

I compared my work to other examples that did work and found no difference at all in the template, the HTML, nor the code.

Long story short, I noticed in some article on the subject that the multiple transclusion feature was added to Angular starting 1.5. I went to check my bower.json and indeed, I was on angular 1.4.8. Upping the version to 1.5 did solve the problem.

Android: From Java to JavaScript and Back

After the fight with Android Studio and Eclipse yesterday, I finally got to try what I was planning to do all day:

  • From within JavaScript running in a WebView, Invoke Android Java code , and
  • From Android Java code, inject JavaScript code into an HTML page in a WebView.

There are a few samples that can be found for both routes, either in the official docs or in blog posts. However, there were some tricks to get even the simplest example running.

Other than the standard issues of having to enable JavaScript in the WebView and annotate the callback method, here are a couple of interesting ones that I faced trying the first route:

  • Default WebViewClient: Attempted to run some trivial JavaScript inside the WebView to make sure it is enabled. The simplest of examples (popping an alert) did not work. After looking around, it turns out that you must set a WebViewClient on the WebView, even if it is simply the default one from the SDK. Apparently, it provides a method that handles showing alerts to make them fit with the theme, etc.
  • Method Signature: The simplest of examples for calling Java from JavaScript did not work for me. I was getting a “Error calling method on NPObject” message. After trying a couple of things I decided to post on StackOverflow, but I had to double check my code before doing so. And guess what I found? I found that the method signature called from JavaScript did not match the one declared in Java. Fixing that got the sample to work.

The second route was not a smooth ride either. Here are a few issues I encountered:

  • Threading: Apparently, any interaction with a WebView has to happen on the ‘same’ thread (as the error stated). I figured that this meant the event thread, and indeed scheduling the injection of JavaScript using runOnUiThread solved this.
  • Navigation: Upon successful execution of JavaScript code, the original page content was replaced by the result of the JavaScript statement that just executed. Apparently, this is to be expected and others have solved this by appending a void(0); to the JavaScript code. The reason is explained here in detail.
  • Arguments: When passing JavaScript code as a String do not forget to quote values that are supposed to be String valued. This simple advice might save you considerable time if you are passing around element IDs. Apparently, when an element ID appeared in the JavaScript unquoted the interpreter took at it as a reference to the element!

Android Studio and ADT on Ubuntu

This was not a fun day.

I spent hours trying to set up Android Studio on Ubuntu. I followed the instructions given here (after eliminating the extra spaces in the URL) and things seemed to be going. After the install, I went through some gyrations to get the Studio to update and run. I imported a project and tried to run it and the IDE started throwing exceptions in the logs. I fought with it a bit, trying to update the SDK, tools, and installing the missing 32-bit C libraries as suggested in this post (only a few are actually needed, it seems, but I installed all just in case). However, new errors kept popping up and I finally decided to go back to what I know; the Android Eclipse plugin.

It was a surprise to me that Google removed the installation instructions for the Eclipse plugin from their site in an attempt to push the Android Studio. And indeed they tout it as the official IDE now and urge people to migrate.

Anyways, I got the plugin from the Eclipse Marketplace. After installing the SDK (again) and trying to run a sample, Eclipse complained about inability to connect to adb, and kept retrying. I went ahead and manually started the adb server from the command line and this did work, at least the first time. Thereafter, Eclipse complained that the connection was lost and prompted me to start both adb as well as Eclipse itself! After having to do this a couple of times, I realized that the SDK path pointed to the first one I installed, which did seem to be broken in some way. Fixing the path got things working again.

Next, I went back to my project but it had compile errors. Apparently, in the initial SDK install I picked (somehow) just API level 17, whereas the feature I wanted required API level 19. I went back and downloaded both 19 and 23 (the latest) which took forever. Finally, I was up and running!