That depends on the device. The Apple iPhone has a builtin accelerometer that handles that function, but it's not available to application programmers - at least yet.
My HTC phone doesn't have motion-detection hardware, but if you slide out the QWERTY keyboard, an event is fired that cause the screen image to rotate. I don't know how to intercept it (it's probably a Win/CE event anyway), but I suspect I could tap into it if I needed to.
Most phones aren't smart enough to know anything at all about how the user is holding them. Newer phones are somewhat more likely to have sensor abilities, however as the cost of hardware continues to drop.
An IDE is no substitute for an Intelligent Developer.