- Working with Textual Input Methods
- Handling User Events
- Working with Gestures
- Handling Screen Orientation Changes
- Summary
- Quiz Questions
- Exercises
- References and More Information
Handling Screen Orientation Changes
Android devices have both landscape and portrait modes and can seamlessly transition between these orientations. The Android operating system automatically handles these changes for your application, if you so choose. You can also provide alternative resources, such as different layouts, for portrait and landscape modes. Also, you can directly access device sensors such as the accelerometer, which we talk about in Chapter 15, “Accessing Android’s Hardware Sensors,” to capture device orientation along three axes.
However, if you want to listen for simple screen orientation changes programmatically and have your application react to them, you can use the OrientationEventListener class to do this within your Activity.
Implementing orientation event handling in your Activity is simple. Simply instantiate an OrientationEventListener and provide its implementation. For example, the following Activity class called SimpleOrientationActivity logs orientation information to LogCat:
public class SimpleOrientationActivity extends Activity { OrientationEventListener mOrientationListener; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mOrientationListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) { @Override public void onOrientationChanged(int orientation) { Log.v(DEBUG_TAG, "Orientation changed to " + orientation); } }; if (mOrientationListener.canDetectOrientation() == true) { Log.v(DEBUG_TAG, "Can detect orientation"); mOrientationListener.enable(); } else { Log.v(DEBUG_TAG, "Cannot detect orientation"); mOrientationListener.disable(); } } @Override protected void onDestroy() { super.onDestroy(); mOrientationListener.disable(); } }
You can set the rate to check for orientation changes to a variety of different values. There are other rate values appropriate for game use and other purposes. The default rate, SENSOR_DELAY_NORMAL, is most appropriate for simple orientation changes. Other values, such as SENSOR_DELAY_UI and SENSOR_DELAY_GAME, might make sense for your application.
After you have a valid OrientationEventListener object, you can check if it can detect orientation changes using the canDetectOrientation() method, and enable and disable the listener using its enable() and disable() methods.
The OrientationEventListener has a single callback method, which enables you to listen for orientation transitions, the onOrientationChanged() method. This method has a single parameter, an integer. This integer normally represents the device tilt as a number between 0 and 359:
- A result of ORIENTATION_UNKNOWN (-1) means the device is flat (perhaps on a table) and the orientation is unknown.
- A result of 0 means the device is in its “normal” orientation, with the top of the device facing in the up direction. (“Normal” is defined by the device manufacturer. You need to test on each device to find out for sure what “normal” means.)
- A result of 90 means the device is tilted 90 degrees, with the left side of the device facing in the up direction.
- A result of 180 means the device is tilted 180 degrees, with the bottom side of the device facing in the up direction (upside down).
- A result of 270 means the device is tilted 270 degrees, with the right side of the device facing in the up direction.
Figure 8.5 shows an example of how the device orientation might read when the device is tilted to the right by 91 degrees.
Figure 8.5 Orientation of the device as reported by an OrientationEventListener.