Day 2

I got an email this morning from my friend Steve from the boards, whose done some experimentation with Android and knows Java really well. I’d sent him an email the night before with some basic questions – basically “where do I start?”. He said that Java was close enough to other, similar languages that I knew, that he’d suggest I jump right into Android-specific development, especially since I’d mentioned that just having the console to test with was very limiting (apparently straight Java can create GUI with something called “Swing” or “awt” – neither of which I’d ever heard of – he suggested leaving that alone and going right into the Android GUI since that’s what I’d be working with at the end of the day anyway).

So I’m abandoning my initial thought of “Java first – Android later”, and jumping right in – however, this will get me out of the console and into a GUI, which was a goal from last night.

I download the android SDK, and follow the very clear step by step instructions. No problem. Move on to installing the ADT Plugin for Eclipse, which facilitates Android-specific development. Again, no problem. Found a quick “Hello World!” Android tutorial, and jumped right in – no problem… With the code at least… First, a quick caveat: the Android emulator sucks. It sucks epically. It takes minutes to launch itself, and minutes to launch any run applications. The laptop I’m using isn’t the fastest, but even if it were 10 times faster I’d still be sitting on my thumbs for a couple minutes every time I wanted to test a change. I needed to be able to move much more quickly. So began the quest to debug through my device (a Samsung Galaxy SII), through a small application that comes with the SDK called “adb” (android debug bridge). This process was awful. Similar issues (although with different devices) are mentiond several times on, and all seemed to have had a successful conclusion. Not so for me – I tried literally everything mentioned, to no avail. ADB would not recognize my device. If I ran the emulator, it recognized that, but nothing I did would make it notice my shiny little phone. I downloaded brand new drivers, restarted ADB, configured my usb drivers manually – nada. Finally, some kid posted a 2 line answer to an older post on SO that was similar: “Download KIES” – wtf is Kies? Kies is apparently some strange creature that – AFAICT – makes Outlook pretend to be iTunes. When nothing else worked, I finally tried it – the software itself actually failed (wouldn’t detect my connection), but that’s fine – it apparently contained the drivers I needed and instantly ADB started working. From there, things went swimmingly. With my phone connected through a USB cord, I can click “Run” from eclipse and instantly see my application on my phone – while not quite as fast as opening a browser window, it’s certainly sufficient.

So, back on track – my “Hello World!” Android app was running properly (and speedily, on my device). Android apps work very similarly to what I’d learned yesterday about Java programming, but rather than a “main” function, it takes an onCreate function with a “Bundle” as the sole parameter (I’m still not sure what that Bundle is). Display objects are called “Views”, but it’s a little different from the typical “V” in “MVC” in that screens are considered “Activities”, which can contain Views – in straight MVC, an Activity would just be another View.

The above just creates a single textfield, writes some text to it, and displays it to the view of the screen (“Activity”).

I grabbed little snippets from various tutorials and pieces of sample code, and tried some conservative changes – creating a button, adding a click event, etc – all went well and as expected – again, the structure is very similar to AS3 or C#. There are Layout elements that work kind of like HBox or VBox elements from Flex (or similar layout structures used in Sencha Touch), which you can use to manage your UI. At the moment, I believe the Activity (screen) can only show one View at a time (using setContentView), so you need to stuff everything else in container views, and add them to another single View element, which is displayed with setContentView – this is different (I think) from addChild in AS3 or C# where you can add whatever you want.

I decided to get a little fancier, and create a ListView component (I’d worked with similar components before in other technologies). This seems a little more complicated than is necessary, but I’ll hold off judgements for a while. Anyway, initially I created a ListView as a reference inside my “document class”, but I couldnt’ get that to work, and the docs state that the “adapter” (basically a connection to a Model) has to be set up during onCreate – which makes me think that any ListView needs to be a ListActivity. So I tried that, and created a button to switch Activities… Boink. After clicking the button, the app crashes with an unknown error. I tried commenting out every statement in the ActivityView’s onCreate method, but that still crashes the app – I believe it has to be either A) the way I’m switching Activities is wrong, or B) the way I’m structuring the ListActivity is wrong. I’m leaning toward the former.

To switch Activities, I’m using the following in a click handler on a Button instance (the click handler works fine, and Toasts* messages appropriately, so the problem has to be in the 2 lines below):

At the point the error occurred, I’m getting sleepy, so I wrapped up for the night, and will try to figure it out tomorrow.