Home >Java >javaTutorial >How to Detect Landscape vs. Portrait Orientation in Android?

How to Detect Landscape vs. Portrait Orientation in Android?

DDD
DDDOriginal
2024-12-06 04:47:11191browse

How to Detect Landscape vs. Portrait Orientation in Android?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn