Imagination Trainer – Developing for older Android versions

This month, I finished work on a brand-new app that jogs your Imagination. Einstein said : “Logic will get you from A to B. Imagination will take you Anywhere”. As a dad, I like to make up bed-time stories. I found it was very hard for the kids to dream up random characters, items and locations for these stories. Those factors combined made this app.

Imagination Trainer serves you with nine random images. Can you make up a story that uses all this pictures? Think outside the box. Try to stretch your story and introduce a plot, an intrige or maybe let your kids wonder what image will make its entrance next?

Programatically, this was not a very big deal. Until I found out that my code wouldn’t run on pre-Honeycomb Android versions! Don’t be mistaking, there are lots of people who won’t update their phones.

Android Versions

My MinSDK was 11, which maps to Android 3.0. This API level was needed, because I used the setAlpha property for ImageView :

 

view.setAlpha(1f);

I actually forgot to set the MinSDK in my AndroidManifest. Within a few hours, complaints started coming in :D. So, I shuffled around some code, and came up with this solution :

public void toggleButton(int i) {
if (indImageBtnEnabled[i]) {

int di = getDrawableId(findViewById(myImagebtns[i]));

Log.v("Button", "Drawable : ");
Drawable d = this.getResources().getDrawable(di);
d.setAlpha(25);

((ImageView) findViewById(myImagebtns[i])).setImageDrawable(d);

indImageBtnEnabled[i] = false;
} else {
// findViewById(myImagebtns[i]).setAlpha(1f);
int di = getDrawableId(findViewById(myImagebtns[i]));
Drawable d = this.getResources().getDrawable(di);
d.setAlpha(255);

((ImageView) findViewById(myImagebtns[i])).setImageDrawable(d);

indImageBtnEnabled[i] = true;
}
}

Also, instead of defining an OnClickListener for all buttons seperately, I decided to do some nice ArrayListing. If you have a lot of buttons (or want to make the number of buttons variable — Not sure, How would you do that?), this looks like a good solution :

	int[] myImagebtns = { R.id.ImageButton00, R.id.ImageButton01,
			R.id.ImageButton02, R.id.ImageButton03, R.id.ImageButton04,
			R.id.ImageButton05, R.id.ImageButton06, R.id.ImageButton07,
			R.id.ImageButton08 };

		for (int i = 0; i < myImagebtns.length; i++) {
			findViewById(myImagebtns[i]).setOnClickListener(this);
		}

Unfortunately, in the OnClick method, I see no possibility to cycle through these buttons (in the switch statement). If you have any suggestion on how to do this, please leave a comment. For now, I’m using :

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnRoll:
Log.v("Roll", " Do stuff here for loading new random images ");

usedImages.clear(); // Clear the arraylist containing the images
resetImageButtons(); // Re-enable all buttons
loadNewImages(); // Load random images
break;
case R.id.ImageButton00:
toggleButton(0);
break;
case R.id.ImageButton01:
toggleButton(1);
break;
// AND SO ON...
// AND SO FURTHER...
// UNTIL WE HIT BUTTON 08
case R.id.ImageButton08:
toggleButton(8);
break;
}
}

Feature Graphic

You can leave a response, or trackback from your own site.

2 Responses to “Imagination Trainer – Developing for older Android versions”

  1. Egon Willighagen schreef:

    “who wont update their phone”

    Or just cannot. There is no update for my 2.3.x phone 🙁

    Egon

    • admin schreef:

      Thanks for your comment. You’re right Egon! Some brands just don’t publish updates :(. Fortunately, the Imagination Trainer now is built to run on all Android versions! 🙂

Leave a Reply