Lost Password?

A password will be emailed to you. You will be able to change your password and other profile details once you have logged in.

Rotational Forces, Part Five

In past posts in the Rotational Forces series, we have seen how to have your activity react to screen rotations, as well as how to force your activity to remain in one orientation (e.g., portrait).

However, we haven't covered the iPhone Scenario.

You may have seen one (or several) commercials for the iPhone, showing how the screen rotates just by turning the device. By default, you do not get this behavior with the T-Mobile G1 -- instead, the screen rotates based on whether the keyboard is open or closed.

However, it is very easy for you to change this behavior, so your screen will rotate based on the position of the phone: just add android:screenOrientation="sensor" to your AndroidManifest.xml file:

[sourcecode language="xml"]
package="com.commonsware.android.rotation.five"
android:versionCode="1"
android:versionName="1.0.0">

android:screenOrientation="sensor"
android:label="@string/app_name">







[/sourcecode]

The "sensor", in this case, tells Android you want the accelerometers to control the screen orientation, so the physical shift in the device orientation controls the screen orientation.

At least on the G1, this appears to only work when going from the traditional upright portrait position to the traditional landscape position -- rotating 90 degrees counter-clockwise. Rotating the device 90 degrees clockwise results in no change in the screen.

Also note that this setting disables having the keyboard trigger a rotation event. Leaving the device in the portrait position, if you slide out the keyboard, in a "normal" Android activity, the screen will rotate; in a android:screenOrientation="sensor" activity, the screen will not rotate.

Mark Murphy is the founder of CommonsWare and is the author of _The Busy Coder's Guide to Android Development_, _The Busy Coder's Guide to *Advanced* Android Development_, and _Android Programming Tutorials_.

Like this? Share it.

Related Posts

Ubuntu Ported To the Galaxy S II

  • 8

The Galaxy S II isn't even officially available yet in the US but it has already been rooted, overclocked and the first custom ROMs are coming up as well. I used to run Ubuntu on my original Galaxy S (Just for fun of course, like you can effectively use Ubuntu on a 4" screen), so I decided to port Ubuntu to the Galaxy S II. Read More »

How To Modify .apk Files

  • 12

The extension Google uses for Android applications may seem a bit complicated, but it really isn't. In fact, an .apk is nothing else than a .zip file disguised as an .apk. That makes things real simple. Read More »

  • http://www.androidactivity.com/ mmoussa

    The behavior that I usually want does not seem like it is possible with the screenOrientation options. For most applications I am seeing that I want the sensor to work when the keyboard is closed, so that if you turn it sideways it will change from portrait to landscape mode. But when the keyboard is open I want it to be in landscape mode no matter what the sensor is. Is there a set of options that would allow me to do this? I think this is only possible using configChanges and managing it yourself, but it may involve a lot more work as the author alluded to in Rotational forces part 3. You may think that the best thing would be to just put it in sensor mode, but the sensor does not always work great, especially if you lay the device flat (which you would do sometimes when typing).

    This rotational mode that I want would be perfect for apps like the browser, where you would read/view in landscape mode or potrait mode, but you would only put input in landscape mode because of the orientation of the keyboard.

  • liz

    did you ever get an answer for this mmoussa?

  • http://passcomms.com/android/?p=14336 Sweeten Your Honeycomb Experience with HD Widgets « Mr. Android

    [...] Rotational Forces, Part Five [...]