Explain Codes LogoExplain Codes Logo

Check orientation on Android phone

java
prompt-engineering
interview-preparation
best-practices
Nikita BarsukovbyNikita Barsukov·Feb 1, 2025
TLDR

Rapidly figure out the orientation of an Android phone by querying Configuration.orientation:

int orientation = getResources().getConfiguration().orientation; switch (orientation) { case Configuration.ORIENTATION_PORTRAIT: // YAY! We are standing tall. Portrait detected. break; case Configuration.ORIENTATION_LANDSCAPE: // Laying flat! Landscape mode it is. break; }

Here, we use the good old switch statement for readability and directly pull the orientation from the Configuration class, making sure you can act on orientation-specific behavior in a jiffy.

Supporting dynamic orientation changes

Android allows our apps to become proper yogis, adapting as the things turn around, literally. For dynamically handling orientation changes in your Activity, we plug an onConfigurationChanged event listener like so:

@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { // A wild landscape orientation appeared! Handle it swiftly. } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ // Back to standing straight like a tower, Portrait. } }

Never forget your AndroidManifest.xml, it needs some love too (and this declaration):

<activity android:name=".YourActivity" android:configChanges="orientation|screenSize"> <!-- Your usual stuff --> </activity>

Embracing all orientations with WindowManager

Our trusty friend, WindowManager, with its handy getRotation method gives us a precise approach, courteous enough to recognize reverse orientations:

WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); int rotation = windowManager.getDefaultDisplay().getRotation(); switch (rotation) { case Surface.ROTATION_0: // Straight as an arrow. The ideal posture. break; case Surface.ROTATION_90: // Look, I'm a landscape now! break; case Surface.ROTATION_180: // Upside down yet fabulous! Reverse Portrait, folks. break; case Surface.ROTATION_270: // A little more spinning and... Reverse Landscape. Whee! break; }

Don't go easy with handling the cases. Remember users are fond of auto-rotation and can do all sorts of acrobatics.

Playing Detective with device physical dimensions

In certain cases, playing Sherlock with your device's screen physical size could help solve the crime of sussing out the orientation:

Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x; int height = size.y; if (width > height) { // Feeling horizontal, eh? Landscape or reverse landscape. } else { // Nice and vertical. Portrait or reverse portrait. }

Though this method gets a bit roundabout, it's a gem when your case involves square or other oddly-shaped displays.

Design considerations for responsiveness

It's the age of "fit to screen". Your screen layouts should not just look pretty but also handle whatever curve orientation changes throw at them.

Using resource qualifiers

Create separate layout files for different orientations. Android will automatically pick the correct one. It's kind of like having two outfits, one for work and one for the gym:

res/layout/main_activity.xml              # For portrait: The work outfit
res/layout-land/main_activity.xml         # For landscape: The gym attire

Reacting to width and height

In situations where the screen is almost square (like a stubborn sandwich), it's better to go by the dimensions. You can create resource files for specific width and height:

res/layout/main_activity.xml             # default
res/layout-w600dp/main_activity.xml      # for screens that are wider than 600dp

Considering square screens

Those square screens always want to be special. Here is how to detect them:

if (Configuration.ORIENTATION_SQUARE == orientation) { // Want to be a hipster square screen? Sure, let's adjust. }