Home » Cool & Future Tech, Featured, General Computing, Headline

Android Development Cheatsheet

7 April 2011 No Comment
Android Development Cheatsheet

Here’s a rundown of everything I ran into when hacking my way through android development.

Setup Eclipse, run the Android SDK installer, install the correct USB drivers and finally check your device setup with “adb devices”. Your device should be listed.

If you don’t have an android device, be prepared for the emulator to be extremely slow especially during startup/shutdown. Be sure to mark your emulator as “Snapshot” (accessible from the ADB/AVD SDK Manager-> Start button). Also, don’t connect your device whilst the emulator is running. Adb gets confused.

I had one occurrence where the drawable resources to my project failed to get copied over properly to the device and I spent 20 minutes trying to figure out what I’d changed in my code. Turning out the emulator was running in the background. I did shut the emulator off and did an adb uninstall “myApp” and then redeployed to fix it.

Android works on Intents, Activities and Services. If you want to access the hardware, e.g. the Camera, you’ll need to fire off an Intent. Here’s an example of launching an Intent to get a Bitmap back from the camera:


You’ll also need to mark your application with the right permissions in the AndroidManifest.xml file. Here’s a good reference to all the Available Permissions.

Use Drawables and Styles in your layout from Day 1. Use RelativeLayout from Day 1 also and attributes such as “layout_alignRight”, “layout_alignTop” as necessary to get the achieved UI. Rather than set a bunch of things invisible/visible, use a Viewgroup such as a nested Relative Layout. Yes you can do Forms this way and if you have a lot of labels/fields to hide/show based on other fields this can be really useful.

Don’t forget you can define gradients and shapes in Drawables also. This saved me a bunch of time trying to create graphics.

In general wrap your root Layout in a ScrollView. This will ensure your screen is usable when you rotate the device.

To Launch a new screen that you need to get something back from (like a Modal dialog) use “StartActivityForResult”, otherwise just use “StartActivity”

Sample code to launch an Activity:

Intent i = new Intent(this, MyActivity.class);
startActivityForResult(i, SCREEN_A);

The constant SCREEN_A just helps you identify in the onActivityResult method which one returned.

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
	super.onActivityResult(requestCode, resultCode, intent);
	if (resultCode == RESULT_OK) {
		Bundle extras = intent.getExtras();
		switch (requestCode) {
		case SCREEN_A: // do something with the return values

Remember your screen can be cancelled by the user using the Back button so only check the intent if the resultCode is “RESULT_OK”, otherwise you’ll get an exception.

Inside your child Activity do this to send data back to the parent:

Intent myReturnIntent = new Intent();
Bundle myBundle = new Bundle();
setResult(RESULT_OK, myReturnIntent);

Use Log.d(TAG, “Something”) combined with “ADB LOGCAT” to debug. Define a TAG constant in every class, it’ll help you filter the Log by defining an environment variable named “android_log_tags” with the relevant tags included. http://developer.android.com/guide/developing/tools/adb.html

If you need to display a Message box, typically use a “Toast” message, here’s a good reference on Toast: http://developer.android.com/guide/topics/ui/notifiers/toasts.html

If you need a dialog box, try this:

public static void ShowAlert(String text, String title, Context activity) {

	AlertDialog.Builder myBuilder = new AlertDialog.Builder(activity);
		.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
                                // just close

	AlertDialog alert = myBuilder.create();

and you can add a “.setNegativeButton” if you need more control.

If you need to store simple string name-value pairs use preferences use the built in UI and Preference manager. Good tutorial is here: http://www.kaloer.com/android-preferences.

If you don’t want to use a Menu you can always use a TabView and I found Google’s example to be complete and easy to follow: http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

For background, long running processing use a Service. Great simple complete example here: http://marakana.com/forums/android/examples/60.html

That should be plenty to help you get your first android app up and running.

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

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

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.