My First Program Using the Android SDK
With all the noise about iPhones, gPhones, Androids, and SDKs floating about, I decided to try my hand at writing an application using the Android SDK. Sticking with my tradition of creating Blackjack games as part of my learning process, I decided to port my Echo2 version of my Blackjack game over to the Android Phone. Much to my delight, Android uses the Java language and made my port extremely easy. First off, here is a preview of my results:

Now for what it took to get it working!
Being proficient with Java and Eclipse makes most of my programming tasks fairly easy and Google did an excellent job at allowing me to leverage both of those skills when developing applications using the Android SDK. First off, I headed over to the Android Getting Started page, http://code.google.com/android/intro/index.html. From there I was guided through the download and installation process for both the Android SDK and the Eclipse Plug-in. The secret sauce was that the Android SDK also comes with an emulator, an essential piece given the lack of phones supporting the SDK. They provide great instructions, so I won’t bother going through the process here.
I went through the Hello World example and was able to quickly get the Android Hello World program working in the emulator. I proceeded by making some minor changes and trying to run the modified application in the emulator and that’s when I ran into a problem. My application would not appear in the emulator. I searched around the emulator looking for a way to run it to no avail. I saw the package in the emulator, but I could not run it. Finally, I went and asked the lord of all answers, the Internet. Fortunately, others had ran into this same issue and while I don’t quite understand the full reason behind it, occasionally the emulator can get into a “bad state.” After issuing an “adb kill-server” and re-running my application, everything worked perfectly. As of writting this entry, this has only happened to me that one time, but it’s good to know that “adb kill-server” will get me back into a workable state should it ever happen again.
With Hello World out of the way it was time to move on to an application that actually did something, albeit something fairly trivial. For me, this was getting Blackjack to work. Having written the Blackjack game code in Java already for other learning projects, I imported my already working Blackjack code into my Android project. Next it was getting the GUI set-up.
For the GUI, I decided that I was start off with just a simple text-only version of my Blackjack Game. Interestingly enough, this is where my limited experience with Flex came in handy. The Android SDK xml markup is very similiar to Flex, where it’s simply a nesting of components, views, and buttons, written in XML using attributes for formatting and setting their various properties. To this day, I am not sure I like the XML approach, but it looks like I better get used to it. Anyway, after creating the markup, initializing the components in the onCreate(…) method of the Activity, and associating game actions with the Button events, I had a functional, text-only, version of the Blackjack Game. At this point, it is my assessment that any one familiar with Swing or Flex and ActionScript could easily pick up the basic GUI aspects of the Android SDK.
I was now at the point where I wanted to add some actual graphics to the application. I have been using the same gif card images for my learning experiments for a few years now and this application was not going to be an exception. However, I had one problem, the images were two big for practical use on the mobile screen. Fortunately, I was able to call on ImageMagick, http://www.imagemagick.org, for my resizing needs. Using the ImageMagick convert command line utility, I quickly resized all of my gifs to 50% of their original size and went ahead and converted them to png files while I was at it. Within a few minutes, I was able to add the new card pngs to my project, which then automatically updated my resource file, updating the R.drawable static inner class with ids for all of my images.
Now the images were considered a resource for my application, but the question now was how to I use them. My other Java applications would always dynamically grab the images files based on their image names as it related to the card value, such that a Jack of Clubs would mean that I would load up the cj.gif image. Now that I was using Android, I realized that if I wanted to access the images by their resource id, I would have to take a slightly different approach. However, being that I am lazy, I came up with a solution that didn’t force me to change my basic approach. Using Java Reflection, I came up with the following code snippet to get me what I needed:
// name = "cj" or other text based on card suit and rank
Field f = R.drawable.class.getDeclaredField(name);
int id = f.getInt(nul);
cardImage.setImageResource(id);
Of course, I added some additional exception handling and type safety code around that, but the above is the guts of my solution. After adding some new LinearLayout blocks and some ImageViews, I was up and running with a version of Blackjack using card images. I will add that I was sort of scared using reflection initially since in knowing that Android is not using a standard JVM, I wasn’t sure what would and what would not work.
In time, I am sure I will find better ways of doing what I did during the implementation of my first program using the Android SDK. My first impressions are positive, but I still need to dig deeper into the various APIs of the Android SDK to see what cool stuff it will enable me to make the phone do amazing things.
For reference, my entire Eclipse Project, including the source, resource files, etc., for Android Blackjack can be found at: http://www.bumblemachine.com/downloads/AndroidBlackjack.rar
Be warned, this project contains my usually hack and slashery associated with my rapid learning of new APIs (that means poor to no comments, poor naming, and quick hacks to get it working.)