Android Postcode app

By: on September 26, 2010

I’ve been playing around with Android development recently, and one of the apps is now worth sharing with everyone else. It’s not particularly complex, but last I looked, there wasn’t an app to do this: tell me what my postcode is. Given you’re holding a phone that knows where it is, and that can talk to the web where there are various services that will tell you what the postcode is for a given location, why not tie these things together? So, I built a little app that does all of this. It looks like the screenshot to the right, and that QR code you see is the market link to download it, or read on for more details about the development of it.


Android development has turned out to be remarkably nice, especially given I’ve done both a lot of embedded systems work and then tried to get stuff working on my old Symbian phone (short answer: don’t bother, it’s deeply frustrating to work with), and so this was a breath of fresh air. There was a few interesting items along the way however:

1) Location lookup:

First we need a location. You’ll need to cope with the fact that potentially the GPS could be switched off (the Android compatibility definition ensures there will be GPS, but not that it’ll be on), and there could potentially be other location providers, so it’s nice to use those as well if they’re any good. So, we dig through the LocationManager, wake up every provider we can find with getProviders() and tell them to give us some data ASAP. There are probably ways to do this more nicely, but given that we not only need to get accurate data, we need to make sure it’s not that old (which we can’t actually select on unfortunately, but we can ignore incoming data that’s too old), the easiest way is just to tell everyone to give you data, and then switch everything back off again once we’ve got the data. On my Desire, when outside or at least not in the bowels of the Tube, I tend to get location data in a few seconds like this. I also setup a timer for a minute, so if we’ve kept trying for that long and nothing good comes up, we throw up an error message and tell the user to try again later.

2) Postcode lookup

So we got a latitude/longitude, and it’s fairly recent, but now we need that translated into a postcode. Luckily, since the release of the OS OpenData dataset, which can be munged to get lat/long -> postcode, a few web services have popped up that’ll do this for us. They’re not all very reliable though, and given they’re web services, it’s entirely possible they’ll go away or change their API at some point, so we have a list of different services to try, and if anything goes wrong we just skip a service and try the next one. Current order is

  1. What is my postcode?
  2. Geonames “find nearby postal codes”
  3. UK Postcodes

Theoretically all 3 could go down eventually, but that should be enough for a bit. Ordering is based on my rather crude estimates of which ones have been most reliable/best data during my testing of the app. Of course, the phone’s data connection could not work, but again we throw an error and suggest you get the data connection sorted out if this happens.

3) Displaying the text

I started off just putting the text into a simple TextView, but it wasn’t very big and a bit difficult to read at the default font sizes. I could have started fiddling around with the font size and gotten something that would look good on my phone, but given I’d like this to be at least slightly cross-compatible, I ended up writing a revised TextView called AutoScaledTextView that automagically resizes text so it’s as big as possible given the amount of space available for the View (Android’s term for the basic displayable widget). It’s a bit kludgy (given non-fixed width fonts), but it seems to work reasonably well and does achieve the goal of displaying the postcodes nice and big.

Overall, this process has been fairly painless. I spent a bit of time figuring out how to do exactly what I wanted, but for the most part I felt Android was helping me along as opposed to being something I had to wrestle with. Provided you’ve got a phone (I’ve done parts of this before I had the phone, but it’s a bit nastier that way), getting yourself up and running and building pretty complex apps is quite easy, and I’ll be posting about another one of those that reuses the data we’ve gathered here today in another way later on. Code for all of this is on Github as usual, or you can just download it from the Market. Enjoy! I hope to see some reviews/bug reports soon :)



  1. Eric H says:

    Just tried it (on a Nexus One running Android 2.2.1). All it does is say “Unknown location”. My GPS is on and working (at least, according to “GPS Status” by EclipSim). I’m in Seattle, WA, US.

  2. Frank Shearar says:

    Have you written anything for the iPhone before? Any comparisons?

    I maintained an application for the Nokia 9110, and found the experience… not pleasant.

  3. Frank Shearar says:

    @Eric The code’s for UK post codes. ( doesn’t cover the US, as far as I can tell.)

  4. Ciaran says:

    Worked perfectly for me. A handy addition to the toolbox – thank you.

  5. Tom Parker says:

    @Eric: I had considered looking into the non-UK problem, but hadn’t dealt with it properly yet. Geonames at least should be able to get postcodes for that, so I’ll play around with a few locations and see if I can improve it a little…

    @Frank: Nope. I’d have to buy a Mac to do so AFAIK, so it’s not really of interest to me at the moment. Making it hard for devs to play around with your system is annoying. That, and if I did this for the iPhone you wouldn’t be seeing this app for another few weeks while it winds it’s way through Apple’s approval process :)

  6. Lee Wei says:

    I’ve written a solver for the game “Swap Me” on the iPhone (basically just perform a brute-force search of the game solution space) Hosted on Google Code @

    I’ve also written a client-server app for the Android after that (basically it’s meant to be like a buzzer system ie. 4 clients to a server, the clients just have one big button to tap, and the server records the sequence (up to a maximum of 4 each time) of incoming taps) – unfortunately the source code’s buried somewhere in my Dropbox.

    I may own a MBP, but I’m not really a big fan of Objective-C, find it much easier to code in Java/Eclipse +AndroidSDK. The GUI design seems easier too…

  7. dariusj says:

    thank you TomP,
    works greate on my GS2

  8. Rooster says:

    Is this app still working? I use it all the time but now it finds location but fails to look up postcode. on WiFi or mobile data.

    I tried to email the address on the Play store but it bounced back.

    Has the API gone down? Will the system be fixed?

  9. Tom Parker says:

    Hmm, yeah. Judging from,-0.0822173.json at least one of the backends has gone down. Patches welcomed, or I may eventually get around to having a look at it…

Leave a Reply

Your email address will not be published.


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>