10 September 2010

The Incredible Shrinking Android

I've been porting the forget-me-never app to Android. To avoid reinventing the wheel, I copied its mail-account-configuration and mail-fetching functions from k9mail. They worked just fine, except for one odd problem: when I used the account-configuration wizard, every time I advanced from screen to screen, the widgets got smaller and smaller. If I toggled back and forth between two pages, I could get them to shrink to the point where the text was completely unreadable.

Strangely, this only happened on the Droid -- it didn't happen in the emulator. (One difference might be that the emulator was running Android 2.0.1 and the Droid was running 2.2.)

I isolated the offending code by the time-honored technique of shotgun debugging: removing everything from the program until the problem went away, then adding thing until I found the culprit. I had thrown away just about everything when I landed on this code in K9Activity.java.

public void onCreate(Bundle icicle, boolean useTheme)
setLanguage(this, K9.getK9Language());
if (useTheme)

// Gesture detection
gestureDetector = new GestureDetector(new MyGestureDetector());


Commenting out setLanguage fixed the problem. That was odd, I thought, what would i18n have to do with screen scaling? Here's setLanguage:

public static void setLanguage(Context context, String language)
Locale locale;
if (language == null || language.equals(""))
locale = Locale.getDefault();
else if (language.length() == 5 && language.charAt(2) == '_')
// language is in the form: en_US
locale = new Locale(language.substring(0, 2), language.substring(3));
locale = new Locale(language);
Configuration config = new Configuration();
config.locale = locale;
The last line is the key. It appears to retrieve a DisplayMetrics object from the context (whatever is subclassing K9Activity, in this case) and then passes it back to the context a second time. Somewhere in that reapplication, a scaling factor is getting applied iteratively, because each time a new actvitiy opens, onCreate gets invoked and the widgets get smaller.

I don't know if this is a bug in the k9mail trunk or just an odd interaction with my phone---I'm using an older version of k9mail that I patched for my purposes and am disinclined to replace it just to test this.

No comments:

Post a Comment

About Me

blog at barillari dot org Older posts at http://barillari.org/blog