Sunday, June 5, 2011

Creating my first Android app, Part 4

This is the last post in a four part series. Go here to read Part 1, Part 2, and Part 3.


This is the final blog about creating apps. First, I'm going to clean up some functionality from the last blog. Second, I'm going to show you how to email a note.

If you "packaged" the app and downloaded to your phone, you would notice it worked, but notes would not be saved between instances of the program. Actually, the notes are saved, but the list of notes aren't.

Here's the solution. We need to store the list of note titles in the database every time we save a note, and then load them every time we try and load a note. It involves modifying two blocks. First, modify the [ButtonNoteSave.Click] block:

  1. Click [My Blocks], click [DatabaseNotes], click+drag [call DatabaseNotes.StoreValue] just below the [call add items to list] block.
  2. Click [Built-In], click [Text], click+drag [text text] to the "tag" slot. Click on the second "text" on the [text text] block, which allows you to edit it. Rename it "TagNoteTitles". This will be the tag where we will store the list of Note Titles. Confusing enough for you?
  3. Click [My Blocks], click [My Definitions], click+drag [global ListNoteTitles] to the "valueToStore" slot.
Now when you save a note, the current list of note titles will be saved into the database. That way, if we close the app and start it again, that list of titles is stored. As I said before, there is no easy way to extract what exactly is in the database, so we need to keep track of it.

Second, we need to load the list. This can be done in several different places, but for simplicity, lets do it before we load a note. We need to modify the [ListNoteLoad.BeforePicking] block.
  1. Click [My Blocks], click [My Definitions], click+drag [set global ListNoteTitles to] just above the [set ListNoteLoad.Elements to] block.
  2. Click [My Blocks], click [DatabaseNotes], click+drag [call DatabaseNotes.GetValue] to the "to" slot.
  3. Click the block we created above named [text TagNoteTitles]. We want to copy it, so press CTRL+C, and press CTRL+V. This should create a duplicate of this text block which we can drag over to the "tag" slot on the [call DatabaseNotes.GetValue] block.
So before we load a note, the list named "ListNoteTitles" is pulled from the database.

Emailing a note

On to being able to email yourself (or some other person) a note. The biggest problem I had with other note apps was the notes were trapped on the phone. To get them off, you had to cut and paste the note into an email.

The basic idea is the note title is the email subject, the note message is the email body, and the email address in the TextBoxEmail is where the email is to be sent. The app will call a component called Activity Starter (which we named AppEmail). Activity Starter can be used to start the camera, open the browser, open a map, etc.

The steps are below:
  1. Click [My Blocks], click [ButtonEmail], click+drag [when ButtonEmail.Click] to the editor.
  2. Click [My Blocks], click [AppEmail], click+drag [set AppEmail.Action] to the notch.
  3. Click [Built-In], click [Text], click+drag [text text] to the "to" slot. Edit the text to say "android.intent.action.VIEW".
I'll be honest; I'm not sure what this does exactly. But I read it on another blog and it works. Did I shatter any ideas you had that I wasn't a hack? Good. Now we have to pass all the interesting information to the Activity Starter. This involves a huge string of text with the email address, subject, and body.
  1. Click [My Blocks], click [AppEmail], click+drag [set AppEmail.DataUri] to the notch below [AppEmail.Action].
  2. Click [Built-In], click [Text], click+drag [call make text] to the "to" slot.
  3. Click [Built-In], click [Text], click+drag [join] to the "text" slot. The [call make text] block will create a new open slot named "text". Click and drag two more [join] blocks to the [call make text] block so there are three total.
  4. Click [Built-In], click [Text], click+drag [text text] to the [join] blocks. It needs to go to the first slot. Do this three times for all three [join] blocks. Edit them to say "mailto:", "?subject=", and "&body=".
  5. Click [My Blocks], click [TextBoxEmail], click+drag [TextBoxEmail.Text] to the top right [join] block. The other two need to be filled in with [TextBoxTitle.Text] and [TextBoxMessage.Text]. I think you are getting the hang of this by now.
  6. Click [My Blocks], click [AppEmail], click+drag [call AppEmail.StartActivity] to the notch below [set AppEmail.DataUri].
When the [Email Note] button is pressed in the app, your email program will come up ready to send the note. All you have to press is [Send]. (Your phone may ask you to specify which mail program to use, if you have more than one.)

So there you go! You have a useful app for recording, playing, storing, and emailing notes. You can go on to add a bunch of extras, like bitmaps for the buttons, icons for the app, etc. to make your app look really pretty (see the picture above for my finished app -- note the gaudy yellow/red/blue color scheme). If you have any questions, please feel free to post comments on my blog. They are moderated (only because I was getting a bunch of Chinese spam) but I usually respond within a day or so. And by "so", I mean weeks or months later. Or never. These are all acceptable definitions of "so'.

I may, at sometime in the future, decide to bore everyone again, and post some of the games I've made. Or maybe not.


No comments: