Update: Please note that this tutorial was written over a year ago. ZXing has moved on quite a bit since then as have the Android Developer Tools. I haven’t had time to revisit and update the post but it should give you a good steer in the right direction. Some of the comments at the bottom may be able to help you out if you encounter any troubles.

Edit: Sean Owen, one of the developers for ZXing has posted a comment to this blog warning of the pitfalls of integrating ZXing into your own app; doing so just to avoid having your users take that extra step of installing from the market is not a good reason. I completely agree with this. There are many advantages to using the intent based approach as outlined in his comments. My motivation is for an enterprise app that does not have access to the Android market and involves my client installing zxing manually on thousands of devices before they are able to distribute to its business customers.

So to be clear, do not use this method unless it is absolutely necessary, and if you do have to – make sure that override your intent filters so that other apps that want to use zxing do not end up calling your modified version. Also, if zxing is already installed, then you should use that by default instead of your modified version.

ZXing is one of the most popular barcode scanning applications on the market. They make it very easy for you to integrate into your application via an intent but this means that your users must manually install the application from the market.

Fortunately, the app is also open source so I will show you how to cleanly build this capability into your project.

Please note that the awesome developers of this product have released the src under the Apache v2.o license so please be sure to adhere to the terms of this license and give them full credit for their work. http://www.apache.org/licenses/LICENSE-2.0

Step One: Obtain the zxing src code
The src can be found at http://code.google.com/p/zxing/source/browse/trunk. Specifically you only need the android/ and the core/ projects. Use svn to checkout these to your local hard-drive.

Step Two: Build zxing core using Apache Ant
You will need to build the core project into a jar file using apache ant (download from here http://ant.apache.org/ivy/download.cgi). Using a shell or cmd prompt navigate to the root directory of the downloaded zxing src and execute “ant -f core/build.xml”. This will produce a file core/core.jar which we will use in the next step.

Step Three: Build ZXing Android using Eclipse
Create a New Android Project (File –> New –> Android Project).
Set the project name to ZXing (or similar).
Select the “Create project from existing source” radio button
Click “Browse” and navigate to the android project that you downloaded from zxing and click “OK”
Select “Finish”

The project will not currently build. We need to add the core.jar file (that we produced in the previous step) into our project. Right-click on ZXing project –> properties –> Java Build Path –> Add External Jars –> Navigate to and select core.jar –> Open –> OK.

Actually, while we’re here we should do one more very important thing! Right-click on ZXing project –> properties –> Android –> Scroll down and check/tick the “Is Library” checkbox –> OK.

Step 4: Include ZXing Android into your project.
Within Eclipse,  Right-click on YOURPROJECTNAMEHERE project –> properties –>Android –> Scroll down to Libraries section –> Click Add –> Select ZXing (which should appear as an option as a result of completing previous step).

Next, in some trigger function e.g. button press within your code you should add:

Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);

In the same activity you’ll need the following to retrieve the results:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
   if (requestCode == 0) {
      if (resultCode == RESULT_OK) {
         String contents = intent.getStringExtra("SCAN_RESULT");
         String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
         // Handle successful scan
      } else if (resultCode == RESULT_CANCELED) {
         // Handle cancel

Almost there! One of the current limitations of Android Library projects is that it will not pull anything from AndroidManifest.xml into your project.
So if we try to invoke the above code we will receive a runtime exception because your Android app has no idea how to handle the scan intent.
To fix this you just need to copy the following into your AndroidManifest.xml:

<activity android:name="com.google.zxing.client.android.CaptureActivity"
      <action android:name="android.intent.action.MAIN"/>
      <category android:name="android.intent.category.DEFAULT"/>
      <action android:name="com.google.zxing.client.android.SCAN"/>
      <category android:name="android.intent.category.DEFAULT"/>

And as Columbo would say, “Just one more thing!”. Add this permission to the top of your AndroidManifest.xml:

<uses-permission android:name="android.permission.CAMERA"/>

EDIT: You need to do yet one more thing! You need to add the core.jar (produced in Step two) to your new project (Right-click your project –> Properties –> Java Build Path –> Add External JARS… –> Select core.jar –> OK). Thanks to Marco and Markosys in the comments for spotting and pointing out the omission!

I use Windows for Android development because I find Eclipse too slow and clunky on the Mac especially on the MacBook Air. Recently I started using GIT for a project having used Subversion exclusively in the past. Given these constraints (Windows and Subversion experience) TortoiseGit seemed the obvious choice for me (I’m not trying to impress my friends with my command line skills any more!).

1) Download and install the lastest TortoiseGit MSI installer from http://code.google.com/p/tortoisegit/
Google code screenshot

2) Create your repository over at GitHub (Assuming you already have an account)
GitHub create repo screenshot

3) Initialise your project as a Git repository by right clicking on your project directory and selecting “Git Create repository here” from the context menu. A .git folder with all the necessary setup will be created within your project folder.
Init git repo on filesystem screenshot

4) Open your project folder and select the files and folders that you wish to include in your git repo (you will want to exclude the /bin and /gen folders for your Android projects). Then right click on these selected items and select “Git Commit -> master …” from the context menu.
commit files screenshot

5) A commit dialog will appear. Write your commit message and select the “select / deselect all” checkbox at the bottom to ensure that all your files are selected. Press OK. It is important to note at this stage your commit is being held locally on your PC as we have not yet linked our filesystem repo with github.com.
git commit msg screenshot

6) Before we can “push” our local commits (plural because you can make many commits locally before pushing them to the server) we must create an ssh key. The easiest way to do this is to open the Git Gui.  Right click on your project folder and select “Git Gui” from the context menu.
Open Git Gui screenshot

7) In Git Gui (notice our unstaged changes on the left), select Help –> Show SSH Key.  A dialogue will appear with your SSH key (if you have one). Most likely you do not have one yet so click the “Generate Key” button and enter a passphrase/password to secure your key when prompted. Your generated key will now appear in the textbox; copy it to the clipboard.
Create SSH Key Screenshot

8 ) Now you must add your ssh key to your github.com account.

a) Sign in to your account at github.com
b) Click the “Account Settings” link (top right of page)
c) Click the “SSH Public Keys” link (left middle of page)
d) Click the “Add another public key” link
e) Give your key a name e.g. github-<your_username> (you might have keys for other things!)
f) paste your key (that you copied to the clipboard in the previous step) into the textarea.
g) click add key
github.com ssh key setup

9) Now that you have created your ssh key and shared it with your github account, we are ready to push our commit (that we made in step 5) to our online github.com repo.  Right click on your project and select “Tortoise Git” –> “Push” from the context menu.
Push to Git screenshot

10) A dialog will appear asking where you would like to push the commit(s) to.  If you navigate to your project on github (e.g. http://github.com/damianflannery/MyProject for me) you will find an ssh url for your project (git@github.com:damianflannery/MyProject.git for my project). Copy your ssh url and paste it in the “Arbitrary URL” field of the dialog and click OK.  You will be asked to enter your passphrase/password for your ssh key and then all being well you will receive a success message.
Git Push Dialog Screenshot

That’s it! Head over to github.com to see your code.

1) Open up Window->Preferences->XML->XML Files->Editor

Eclipse Preferences Dialog

2) Check the “Split multiple attributes each on a new line” and adjust other options according to your liking (such as Indent using spaces and Indentation size)

3) Press OK to save the options

Now, all you have to do is press Ctrl-Shift-F or select Source->Format to produce a more readable and enjoyable programming experience like this:

Android XML file

Debugging in Eclipse

March 25, 2010

This morning I discovered an excellent step by step (excuse the pun) series of video tutorials on how to use the debugger in Eclipse. I was looking for a good learning resource to help teach programming to a colleague but actually I learned a few things myself while watching the tutorial set :p.

This is an excellent series for those who are new to debugging or those that are new to using Eclipse.