Handling Screen Orientation Changes Android

Many Android devices on the market today have 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 (more on this in Chapter “Targeting Different Device Configurations and Languages”). Also, you can directly access device sensors such as the accelerometer, as we talked about in Chapter “Using Android’s Optional Hardware APIs,” 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 Orientation Event Listener class to do this within your activity.

Implementing orientation event handling within 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 andother 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. (What “normal” means is defined by the manufacturer. You need to test on the device to find out for sure what it means.)
  • A result of 90 means the device is tilted at 90 degrees, with the left side of the device facing in the up direction.
  • A result of 180 means the device is tilted at 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 at 270 degrees, with the right side of the device facing in the up direction.

This shows an example of how the device orientation might read when the device is tilted to the right by 90 degrees.

Orientation of the device as reported by an OrientationEventListener.

Orientation of the device as reported by an OrientationEventListener



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Android Topics