Thursday, January 20, 2011

Build.VERSION.SDK_INT on Android 1.5

It won't be long before all Android 1.5 devices will disappear, until then it's a good idea to support them. The problem is the 'Build.VERSION.SDK_INT' was introduced only on Android 1.6, and before that there were only string values.

Here's a work-around this issue - it will return the correct value for Android 1.5+:


Note: the internal class is required since the Java virtual machine checks if a class is valid before loading it. In our case, a java VM running Android 1.5 will not be able to load Build.VERSION.SDK_INT and will crash.

UPDATE: Added VerifyError catch to prevent exceptions on some weird Android 1.5 devices.

Friday, December 24, 2010

Finding Android screen orientation

There are multiple way to get Android screen orientation (landscape/portrait), however some of them are problematic:
this.getWindowManager().getDefaultDisplay().getOrientation()
Is depricated

this.getWindowManager().getDefaultDisplay().getRotation()
Works only from Android 2.2

I Found this to be the best resource until now, since it's exists since API 1 & it's not deprecated, yet:
this.getResources().getConfiguration().orientation

Compare it with:
Configuration.ORIENTATION_LANDSCAPE
Configuration.ORIENTATION_PORTRAIT
Configuration.ORIENTATION_SQUARE

Tuesday, December 14, 2010

Spinner.setSelection doesn't work correctly

Problem:
There are a lot of posts around regarding a problem with Spinner.setSelection(i) function. I encountered this issue when I've tried to replace the adapter in the Spinner and select a new item. The setSelection function seems like it's not working, but when I click the spinner the selected item was the correct one.

Solution:
Use Spinner.setSelection(i, true) instead

Wednesday, December 8, 2010

Multiple substitutions specified in non-positional format

Since Android SDK 2.3 there's a new error:
error: Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute?

Description:
This error indicates you're using a string resource with %s in it. See how to use it here under 'Formatting strings'. In general, instead of:
Hello, %s! You have %d new messages.
Should be:
Hello, %1$s! You have %2$d new messages.

Tuesday, December 7, 2010

Can not find adb.exe after Android SDK update

After updating the Android SDK I got the following message when I opened Eclipse:
Can not find C:\Program Files\Google\Android SDK\tools\adb.exe
Please check the Android plugin's preferences

And when I checked the adb.exe was really missing!

Solution:
Update the Android add-ins for Eclipse, in Eclipse: "Help" -> "Check for Updates".

The reason for this error (in my case) was that since SDK 2.3 the 'adb.exe' was moved to:
C:\Program Files\Google\Android SDK\platform-tools\adb.exe

UPDATE: ddms.bat does not work as well (can not find adb.exe). The work-around I'm using until I'll find a better solution is adding the platform-tools folder to the environment PATH variable.

Friday, November 26, 2010

AdMob as shows in Landscape but not in Portrait

This was really weird. I added AdMob Ad to an Android application, and it didn't show. I tried rotating to landscape & the ad appeared.

Solution: I found the solution here - the AdMob ad must not be padded, or be in a padded container.

Monday, November 8, 2010

Android application installation stuck on "Downloading"/"Installation"

Problem: Application installation hangs/stuck on "downloading" or "installing" from the Market. Reboot does not help.

Solution: Clearing the Android Market application cache -
* Menu => Settings => Applications => Manage Applications => "All" tab => Market
* Click on "Clear cache" (you might get 'Force Close' on the Market application after that).
* Reopen the market & download

Note: This is an issue with the Android Market application not the application you're downloading.