Determining Device Orientation on Android
When developing Android applications, it's often necessary to adapt the user interface based on the device's orientation. To determine whether the phone is in landscape or portrait mode, use the following code:
int orientation = getResources().getConfiguration().orientation; if (orientation == Configuration.ORIENTATION_LANDSCAPE) { // In landscape } else if (orientation == Configuration.ORIENTATION_PORTRAIT) { // In portrait }
The getConfiguration() method retrieves the current configuration, including the orientation. By comparing the orientation variable to the constants defined in Configuration, you can determine the device's current orientation.
Additionally, the developer documentation provides detailed information on orientation and other configuration values: https://developer.android.com/reference/android/content/res/Configuration
The above is the detailed content of How to Detect Landscape vs. Portrait Orientation in Android?. For more information, please follow other related articles on the PHP Chinese website!