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();
bitmap.recycle();
img.setImageResource(imgID[currentImg]);

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

Advertisements
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:

android:configChanges="orientation|keyboardHidden"

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

Also, add this to the Java class:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
    	super.onConfigurationChanged(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:

android:launchMode="singleTask"

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);
startActivity(i);

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 developer.android.com/reference 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  

The Pain of Developing for Android

Make no mistake about it. Developing for Android is a f*cking PITA. The people who created Android had no conception of ease of use, user-friendliness, or high productivity. Their only goal was programming flexibility. Typical hacker mentality.

I recently started to write an Android version of an iPhone app. Writing for iOS, using Xcode, is a breeze compared to using Eclipse and ADT. The Android environment is extremely complex (remember, the people behind Android were only interested in flexibility, elegance be damned).

Here are the three biggest gripes I have about Android development:

  1. The Android emulator is unbelievably slow and generally unreliable. It takes several minutes to upload an application to it!! And starting up the emulator fails about half the time (the dreaded “emulator disconnected” error).
  2. Each step in adding functionality to the app is like pulling teeth. Nothing is easy nor straightforward. The SDK has voluminous documentation, but it is tough slogging to get through, and there is a dearth of good examples.
  3. ADT’s counterpart to Xcode’s Interface Builder is crude, unintuitive, incomplete, and frustrating to use. Trying to visually layout your application screen is so painful, it nearly pushed me to suicide.

I’ve overcome a number of obstacles that I’m sure most novices to Android development would normally encounter, so I have created this blog to help them.

Published in: on January 8, 2012 at 3:45 pm  Comments (1)