An iPhone Developer Learns Android: Some Thoughts On Code

images

Here are basics to coding in Java/Android for Eclipse.

By Anna Billstrom (Developer, Momentus Media)

At first, doing a “Hello World” was quite easy.

The hardest part was learning Eclipse (for Mac):

  • Open Eclipse and don’t worry about opening a project, on the left hand side will be all of your “workspace” projects.
  • Running (the play button) does an automatic build.
  • Mouseover red squiggly underlines to find build errors.

I lost one of my panes, and that took forever to learn how to open again. I still can’t confidently tell you how (sorry).

Some basics to coding in Java/Android for Eclipse:

  • To log, you have a few options: import “import.android.util.Log” and write the following: Log.e(“onestring”,”anotherstring); – You can do Log.e (error- it’s red in Log pane), “d”, or “a” (assertion).
  • To add resources (media, data files, etc.) in iPhone, you drag resources to your project in Xcode. For Eclipse, copy to the “/res/raw” directory, with no mention anywhere else. I assume this gets indexed on compile.Layout is still a total drag. The general idea is you can programmatically create your layout or use an XML file, and update values from code. I have to create 25 objects, so it’s programmatic.
    • Good news: compared to iPhone, there are a lot more settings and variances.
    • Bad news: there are different ways of building the objects so it’s ending up being far different than my iPhone version.

    Still mastering things like “centering a button” “saving state” “animating a fade” etc. May give up on some of those bells and whistles for the first version.

    Disappointed with file reading in Android – it’s quite easy in iPhone, you can map a file directly to an NSDictionary object. I’ll include my code here for parsing a simple key-value data file:

    final Hashtable<String, String> wordsHT = LoadText(R.raw.dict1a);

    public Hashtable LoadText(int resourceId) {
    // The InputStream opens the resourceId and sends it to the buffer
    InputStream is = this.getResources().openRawResource(resourceId);
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String readLine = null;
    Hashtable<String, String> tmpHT = new Hashtable<String, String>();

    try {
    // While the BufferedReader readLine is not null
    while ((readLine = br.readLine()) != null) {
    String[] wordA = readLine.split(“:”);
    tmpHT.put(wordA[0], wordA[1]);
    }

    // Close the InputStream and BufferedReader
    is.close();
    br.close();

    } catch (IOException e) {
    e.printStackTrace();
    }

    return tmpHT;
    }

This post was originally posted at Banane.

Editor’s note: Got a question for our guest blogger? Leave a message in the comments below.

About the guest blogger: Anna Billstrom is a developer with Momentus Media, a startup in the Mission District of San Francisco specializing in helping brands go viral. Their recent app “8 Bit Your Pic” for Black Eyed Peas saw 2 million users in 2 weeks. She’s done the gamut of OLAP DB modeling to iPhone development and Ruby on Rails. Currently, Anna is enjoying the fast lane of rapid, viral app development on Facebook. Contact her at [email protected] or follow her on Twitter at @banane.