Programming and general geekiness.

Archive for February, 2012

Your Country Needs You v1.1


I’ve done an update for Your Country Needs You which brings it to version 1.1 on the Android Market. It now allows you to modify the background color which seemed to be a logical progression and the only reason that it wasn’t in the original version was because I completely forgot to add it in. In a future version I do intend that it will be possible for you to modify Lord Kitchener’s face however he is sticking around for the time being.

Your Country Needs You on the Android Market

Keep Calm v1.2.2

This isn’t really a major update to Keep Calm for Android however it finally fixes the issue of posters not saving properly. It turns out that they were saving all along, however the Gallery app doesn’t refresh the picture list straight away. I’ve written up an explanation here.

Keep Calm for Android

Free Android app: Your Country Needs You

As a natural follow up to my Keep Calm and Carry On app it made sense to develop another similar app based on another famous British wartime poster. This new app allows you to modify the text of the poster however you will soon be able to change Lord Kitchener’s face to your own face and also change the background color (this update will probably ship in the next week).

The app also fixes the saving error in the Keep Calm app* – it turns out that your posters were saving but not always appearing in the gallery. This was a technical error in that the gallery app doesn’t check for new images when it loads and so adding one extra line of code in my app automatically refreshes the gallery. As well as saving you are also able t0 share, set as wallpaper and choose from three fonts – Alfa Slab One, Enriqueta, PT Sans.

This slideshow requires JavaScript.

Your Country Needs You on the Android Market

*Don’t worry, I’ll be updating Keep Calm so you can save your pictures properly in that too.

Sharing and saving Bitmaps in Android

If you’ve ever worked with Android development before you’ll probably know that once you get it right it can be incredibly rewarding however if you get it wrong it can be a real pain to fix. Sometimes the development also demonstrates the fragmentation of Android and how it can be a great challenge to do something relatively simple because it works differently on different devices.

I recently developed the Keep Calm app for Android that allows users to create Keep Calm and Carry On style posters. The app has been a reasonable success (20,000 downloads in a month) however there has been one major issue: not everyone could save properly. At first I didn’t really understand why this was because all of the testing I had done indicated that the Bitmap object that the image was stored in was being correctly saved to the SD card and it would then appear in the Gallery app. However, this was not the case for everyone.

I soon discovered that it wasn’t a technical issue with my app entirely. The default Gallery app gets a list of files in the various images folders on the SD card each time it loads but the Android system doesn’t always refresh this list of files very often – sometimes only when the device is restarted. Clearly it would have been a major issue for my users to have to restart their phone each time they wanted to view a file so I came up with this solution:

String saved = Images.Media.insertImage(this.getContentResolver(), bitmap, title, description);
Uri sdCardUri = Uri.parse("file://" + Environment.getExternalStorageDirectory());
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, sdCardUri));

The bitmap object is of type Bitmap whereas title and description are both Strings. The sendBroadcast function (your activity inherit it as the class extends Activity, where the function is) then forces the OS to remount the SD card as it would do when the device starts up meaning that your picture will appear in the Gallery app. If you want to prove this to your users you can load up the file in the app:

Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(saved), "image/*");
startActivity(intent);

This then loads the image up, using an Intent, in whatever the default Gallery app is. Remember that you’ll need to add the android.permission.WRITE_EXTERNAL_STORAGE to your Android Manifest otherwise the app will fail. The obvious progression of saving is, of course, sharing and Android makes this incredibly easy to do without having to use the APIs of all the various social networks that exist – instead the app can just utilize services provided by other social network apps on the phone. Here is how to do it:

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(saved));
startActivity(Intent.createChooser(sharingIntent, "Share image"));

This loads up a menu (unless no options are available other than email) headed with the “Share image” text asking the user what service they would like to share it with. The appropriate app will then launch. Another cool thing I’ve done using a Bitmap is to set the wallpaper. You’ll need to ensure that the Bitmap is double the width of the user’s device and the same height (explained in this Lifehacker article) – this could be done using a Canvas. Here is the code you can use:

WallpaperManager wallpaperM = WallpaperManager.getInstance(this);
wallpaperM.setBitmap(newBitmap);

Like with saving, you’ll need to ensure that you add the android.permission.SET_WALLPAPER t0 your manifest otherwise, again, the app will fail. Hopefully this article has helped you with your projects and if you have any other problems leave me a message in the comments.

Prerequisites for Android Development

I really like doing Android development and over the past few months I’ve done quite a bit of it but I found at the beginning that some things didn’t seem to be entirely logical that were quite important despite the fact that I already had a vast programming knowledge. Here is a list of things I reckon are useful for learning Android development:

  • Java experience: I don’t think that you need to know loads of Java to develop Android apps but I would say it is important that you at least know the syntax and roughly how to do things. Experience in a similar language like C# or C++ would probably get you by, just so long as you know the difference between a package, class and that kind of thing
  • Experience in another object-orientated language: Some experience in another language would also be useful because despite the fact that most Android development is in Java it is useful to understand how other languages do things because a lot of the Android specific Java has had influence from C++
  • An understanding of how apps work: This would probably come with programming experience but you really aren’t going to get anywhere if you don’t get how the most basic apps. Reading a few tutorials can help fix this
  • Experience creating user interfaces via code and and a visual designer: Android uses both XML layouts and pure Java code to create User Interfaces so provided that you have done something in both before you’ll probably be fine. I should imagine that it would probably be OK if you’ve done JavaScript DOM and HTML
  • Basic knowledge of XML and SQLite: You won’t need to know how to do these perfectly but so long as you can create XML documents and edit them. I wouldn’t say SQLite knowledge is vital but it would be good to have some database knowledge
  • A basic understanding of other mobile platforms: I had done a tiny amount of iOS and Windows Phone development before starting Android which probably helped me a tiny bit because it gives you some knowledge of design. Windows Phone development is probably more different to Android than iOS, so even if you’ve just created a calculator in iOS you’ll be in a good position to start Android development
  • Good resources for testing: The emulator is good but I’ve found that it always useful to either have at least one Android device for testing. I also recommend setting up a few different devices in the emulator with different screen sizes and versions of Android because that will give you a chance to test in loads of different environments
  • You’ve read some stuff on Android development: It is worthwhile reading up on Android development before you get started. Android Design and the Android Guide are both good places to start and reading some blogs will probably help develop your ideas. If you get stuck, make sure that you go on Stack Overflow.

Once you are fairly confident with all of this I recommend going over to Lars Vogel’s site which has some great development tutorials.