Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check orientation on Android phone

How can I check if the Android phone is in Landscape or Portrait?

like image 996
Mohit Deshpande Avatar asked May 08 '10 22:05

Mohit Deshpande


People also ask

How do I find the orientation of my Android phone?

If the activity locks the display ( android:screenOrientation="portrait" ), this method will return the same value irrespective of how the user rotates the device. In that case you'd use the accelerometer or the gravity sensor to figure out orientation properly.

How do I fix the orientation on my Android phone?

To do this, swipe down from the right side of the top panel. Hold the device in the orientation in which you want it locked. On the drop-down menu, touch the “Auto Rotate” button. The “Auto Rotate” button becomes the “Rotation Locked” button.

How do you know if orientation changes?

use android:configChanges="orientation|screenSize" instead of android:configChanges="orientation|keyboardHidden" in your manifest file and check ... !!!


2 Answers

The current configuration, as used to determine which resources to retrieve, is available from the Resources' Configuration object:

getResources().getConfiguration().orientation; 

You can check for orientation by looking at its value:

int orientation = getResources().getConfiguration().orientation; if (orientation == Configuration.ORIENTATION_LANDSCAPE) {     // In landscape } else {     // In portrait } 

More information can be found in the Android Developer.

like image 114
hackbod Avatar answered Oct 13 '22 22:10

hackbod


If you use getResources().getConfiguration().orientation on some devices you will get it wrong. We used that approach initially in http://apphance.com. Thanks to remote logging of Apphance we could see it on different devices and we saw that fragmentation plays its role here. I saw weird cases: for example alternating portrait and square(?!) on HTC Desire HD:

CONDITION[17:37:10.345] screen: rotation: 270 orientation: square CONDITION[17:37:12.774] screen: rotation: 0 orientation: portrait CONDITION[17:37:15.898] screen: rotation: 90 CONDITION[17:37:21.451] screen: rotation: 0 CONDITION[17:38:42.120] screen: rotation: 270 orientation: square 

or not changing orientation at all:

CONDITION[11:34:41.134] screen: rotation: 0 CONDITION[11:35:04.533] screen: rotation: 90 CONDITION[11:35:06.312] screen: rotation: 0 CONDITION[11:35:07.938] screen: rotation: 90 CONDITION[11:35:09.336] screen: rotation: 0 

On the other hand, width() and height() is always correct (it is used by window manager, so it should better be). I'd say the best idea is to do the width/height checking ALWAYS. If you think about a moment, this is exactly what you want - to know if width is smaller than height (portrait), the opposite (landscape) or if they are the same (square).

Then it comes down to this simple code:

public int getScreenOrientation() {     Display getOrient = getWindowManager().getDefaultDisplay();     int orientation = Configuration.ORIENTATION_UNDEFINED;     if(getOrient.getWidth()==getOrient.getHeight()){         orientation = Configuration.ORIENTATION_SQUARE;     } else{          if(getOrient.getWidth() < getOrient.getHeight()){             orientation = Configuration.ORIENTATION_PORTRAIT;         }else {               orientation = Configuration.ORIENTATION_LANDSCAPE;         }     }     return orientation; } 
like image 21
Jarek Potiuk Avatar answered Oct 14 '22 00:10

Jarek Potiuk