Recycling ImageView Bitmaps

I encountered memory issues when displaying large jpegs (in series) in a slideshow-like activity. I learned that you need to recycle the bitmaps with a call to recycle():

ImageView img;
Bitmap bitmap = ((BitmapDrawable) img.getDrawable()).getBitmap();

Note that ‘imgID’ is an array of resource IDs.

Published in: on January 18, 2012 at 6:26 pm  Leave a Comment  

Orientation Changes

When you change the orientation of your Android phone from, say, portrait to landscape, it can cause problems. I avoided these problems by inserting the following into the activity tag:


Note that “keyboardHidden” is required; it is intimately tied to orientation.

Also, add this to the Java class:

    public void onConfigurationChanged(Configuration newConfig) {
Published in: on January 18, 2012 at 6:15 pm  Leave a Comment  

Ensure Only One Activity Is Created

I found that when my application is pushed into the background (via, say, pressing the Home button) and then resumed, a new activity is created. In order to ensure that the original activity is preserved, add this to the activity tag in the Android manifest:


A key benefit of the above is that if applied to the root activity of your app, you can jump to the “home” screen by starting the root activity. All the other activities above it are popped off the stack and destroyed.

Published in: on January 18, 2012 at 5:26 pm  Leave a Comment  

A Java Compiler Tip

Check the project’s properties and verify that Java Compiler -> Compiler compliance level is set to 1.6. I got burned on this once.

Published in: on January 18, 2012 at 5:06 pm  Leave a Comment  

How to Switch to Another Activity

First, create a new activity by:

  1. creating a new Java class that extends Activity (for our example, we’ll call it Step1Activity)
  2. creating a new XML layout
  3. adding the new activity to the Android manifest XML

Second, from, say, a button’s onClick() implementation, add this:

Intent i = new Intent(getApplicationContext(), Step1Activity.class);

To resolve various names, you’ll need to import a bunch of things. For example, to resolve ‘Intent’, you’ll need this:

import android.content.Intent;

In general, I Google the name (along with “android”) and use the URL at to tell me what the import is.

Published in: on January 18, 2012 at 4:49 pm  Leave a Comment  

Tips for Using the Android Emulator

The Android emulator is slow as molasses in January. If at all possible, get an Android phone for development. I purchased a prepaid, unlocked Google Nexus S from Break Wind Mobile for C$380, a good investment.

If you must use the emulator, you should configure it to use more memory. Go to Run->Run Configurations…->Target tab for your app. For “Additional Emulator Command Line Options”, insert “-partition-size 1024”. You may also want to check off “Wipe User Data” so that from time to time, you can start with a clean slate.

Starting the emulator can be problematic. An “emulator disconnected” error is common. So once you have it up and running, try not to shut it down; you can repeatedly upload your app to the emulator for testing.

I also found that having a command line window open with ‘adb logcat’ running helps to stabilize the situation. In Windows, go to where android-sdks\platform-tools is and run ‘adb logcat’. You can also filter results with ‘adb logcat | find “your_app_tag”‘.

Published in: on January 18, 2012 at 4:24 pm  Leave a Comment