Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to detect orientation of android device?

Is it possible simple to detect current orientation of android device, without programming a listener and handling the position matrix? In my app I want only to know current orientation - vertical or horizontal - at the moment. However I don't want to listen to events of axiometry or other events.

like image 454
Mark Avatar asked Feb 25 '11 00:02

Mark


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 can we detect the orientation of the screen?

Detecting Orientation Changes in Javascript Should you need to simply detect when a user changes orientation, you can use the following event listener: screen. orientation. addEventListener("change", function(e) { // Do something on change });

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

You can also use:

getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE 
like image 78
Francisco Javier Fernández Avatar answered Oct 18 '22 16:10

Francisco Javier Fernández


Use the getRotation method:

Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); int rotation = display.getRotation(); 

From the documentation:

Returns the rotation of the screen from its "natural" orientation. The returned value may be Surface.ROTATION_0 (no rotation), Surface.ROTATION_90, Surface.ROTATION_180, or Surface.ROTATION_270. For example, if a device has a naturally tall screen, and the user has turned it on its side to go into a landscape orientation, the value returned here may be either Surface.ROTATION_90 or Surface.ROTATION_270 depending on the direction it was turned. The angle is the rotation of the drawn graphics on the screen, which is the opposite direction of the physical rotation of the device. For example, if the device is rotated 90 degrees counter-clockwise, to compensate rendering will be rotated by 90 degrees clockwise and thus the returned value here will be Surface.ROTATION_90.

Keep in mind that getRotation was introduced from Android 2.2. Use getOrientation if your target are older devices.

like image 35
Cristian Avatar answered Oct 18 '22 15:10

Cristian