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.