Tuesday, December 15, 2009

Adding Motorola Droid support for my Android games

A couple of weeks ago, a friend bought a Motorola Droid and I found that my Android games did not layout properly due to the higher resolution. The paddings and margins were too small, and fonts were WAY too small. I thought this was going to be a major headache...but it turns out it was an easy fix.

I just needed to change all my units from "px" to "dp" which stands for "density-independent pixels." This was easy to do for the layout XML files, but it was a little different for my code calculated sizes. For images that were based on the percent of the screen size, no change was necessary. However, I had some font sizes that were defined with a size of 16 pixels. The fix? Multiply that value by the value in DisplayMetrics.density()

http://developer.android.com/reference/android/util/DisplayMetrics.html

The density value is 1 for the resolution seen in the HTC G1. For smaller resolutions, you get a scaled value less than 1, and for higher resolutions, you get a value greater than 1. After making these changes to support the high resolution Droid....the game worked perfectly on a low resolution screen (i.e. HTC Tattoo).

I hope this helps others in adding support for high resolution screens to their applications.