Monday, March 8, 2010

Modifying an Alert Dialog's List Items after Creation

Category: UI
Subcategory: AlertDialog, ListView
Android Platform: 1.5, Google APIs 3
Referenced Classes: android.app.AlertDialog, android.widget.ListAdapter, android.widget.ListView

There are cases when you need to change the text of an Alert Dialog's List Items, after the Alert Dialog has already been created. This is typical when working with the Dialog lifecycle of an Activity (i.e. Activity.onCreateDialog(…), Activity.onPrepareDialog(…)). Take the following example:
code snippet
protected Dialog onCreateDialog(int id)
{


final CharSequence[] items = {"Red", "Green", "Blue"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
        Toast.makeText(getApplicationContext(),
items[item], Toast.LENGTH_SHORT).show();
    }
});
AlertDialog dropdown = builder.create();


}
Suppose, following some user action in the Activity UI, you want to append text to the first list item of the drop down. So, instead of displaying "Red", the first list item should read "Red {actionResultValue}". First, you need to modify the above code, accordingly:
code snippet
protected Dialog onCreateDialog(int id)
{


final CharSequence[] items = {
new StringBuilder(),
"Green",
"Blue"}; // or any non-immutable CharSequence


}
Now, when Activity.onCreateDialog(…) is called, the first list item is a pseudo placeholder. Next, set the text of the first list item in Activity.onPrepareDialog(…) with the following:
code snippet
protected void onPrepareDialog(int id, Dialog dialog)
{


String actionResultValue = ...
ListView dropdown= ((AlertDialog) dialog).getListView();
ListAdapter dropdownAdapter= dropdown.getAdapter();
//
StringBuilder listItem0Text=
(StringBuilder) dropdownAdapter.getItem(0);
listItem0Text.delete(0, listItem0Text.length());
listItem0Text.append("Red " + actionResultValue);
//
dropdown.invalidateViews();


}
Note, the call to dropdown.invalidateViews(). Without this, the text changes will not display.

Sunday, March 7, 2010

Using the Maps External Library on an Android Device

Category: Production
Subcategory: Google APIs Add-On, Maps External Library
Android Platform: 1.5, Google APIs 3

During development, configuring an Android Virtual Device (AVD) to use the Maps External Library, is as simple as setting the target to Google APIs API Level 1.x. However, when deploying to production, you are entirely dependent on the Android Device's system image. Consequently, you can spend months developing a Map-based app, that won't run on a device that you are targeting. I was unable to find a published list of supporting Android Devices. Furthermore, the Google APIs Add-On Site states:

"The Maps external library is not part of the standard Android library, so it may not be present on some compliant Android-powered devices. Similarly, the Maps external library is not included in the standard Android library provided in the SDK."

In an attempt to ensure my app would run independent of any device's system image, I tried to bundle the com.google.android.maps.jar directly into my app. I removed the "<uses-library android:name="com.google.android.maps" /> from my AndroidManifest.xml file, and placed the maps.jar directly into my app's lib directory. After succeeded on the build phase I deployed to the AVD. However, an exception was thrown, with some message of "Stub Only". Feeling somewhat discouraged, I looked around for any reasonably priced--preferably free or open-source--third-party Android Map SDKs or libraries. I found two notable ones: Ericsson Labs Mobile Maps and Nutiteq MGMaps Lib SDK. After considering these alternatives, and accounting for the time and effort already invested using the Maps External Library, I decided to go another direction.

Sticking with the Maps External Library, I would compile my own list of supporting Android Devices and only provide app support accordingly. Besides deploying the app to the device, another way to ensure that an Android Device supports the Maps External Library, is to connect to it, using the adb tool. Navigate to the /system/framework/ folder and verify that the com.google.android.maps.jar is there. I suspect that any device running the Google Maps App, would have this jar--I cannot confirm this. Thus far, my list is rather small, but I hope to add more. Please, let me know if you have tested or verified other devices that are not already on this list. I'll be happy to add them.

Android Devices with Maps External Library Google APIs 1.5 Level 3 Support

  • Motorola Droid
  • HTC Eris