0

I have a task where I have to cast device (not activity) orientation to 4 values (LANDSCAPE, PORTRAIT, LANDSCAPE_REVERTED and PORTRAIT_REVERTED). I came up with a solution but it uses fixed values, and does not represent the android functionality of how and when it changes the activities orientations.

I was unable to find an api that could easily notify a listener of when the device has changed it's orientation to the specified 4 values.

It would be grate if someone could share the function of how the android computes the orientation and informs WindowManager to change the orientation of the activities.

[SOLVED] To does who might end up in the same situation as me. frameworks/base/core/java/android/view/WindowOrientationListener.java

this listener contains all the magic that informs the activity to change it's orientation.

Twisted
  • 89
  • 2
  • 7

5 Answers5

1

In onConfigChanges()you 'll always be notified whenever ur screen orientation changes and you can always get orientation of ur screen like this :

Display getOrient = getWindowManager().getDefaultDisplay();
switch(orientation){
case Configuration.ORIENTATION_LANDSCAPE:
Log.e();
break;
case Configuration.ORIENTATION_PORTRAIT:
Log.e();
break;
}
user1969053
  • 1,690
  • 12
  • 12
  • This will read the top most activity orientation. Not the device activity. (And cast it only to two orientations). – Twisted Mar 01 '13 at 11:09
0

Use the screenOrientation in your project manifest and set the orientation.

<activity
            android:name=".SplashScreen"
            android:screenOrientation="portrait"
</activity>
Muhammad Aamir Ali
  • 18,753
  • 9
  • 62
  • 54
  • It's not for the device orientation. – Twisted Mar 01 '13 at 11:11
  • Hi Hoan Nguyen . can you pls share you code base or pls advice me for this post http://stackoverflow.com/questions/25133241/need-help-on-rotating-image-view-with-animation-on-orientation-change-4-side-ro – Kris Aug 05 '14 at 16:34
0

If u want the activity to show and align in landscape mode then add a folder in drawable namely layout-landscape and if u want the app to run only in portrait mode then declare in the manifest as the following

android:screenOrientation="portrait"

or u want in the landscape mode:

 android:screenOrientation="landscape"
Danny
  • 1,050
  • 3
  • 11
  • 26
0

I am pretty sure the OS calculate the rotation of the device and change orientation when the rotation angle change by 60 degrees. Thus if the device is in Portrait orientation with rotation angle equal to 0, then when the device is rotated say counterclockwise, the rotation angle will increase and when the rotation angle is about 60 degree, the OS will change to Landscape. Now suppose the device is in Landscape orientation when you rotate the device clockwise, the angle will decrease and at about 30 degree the OS will change to Portrait orientation.

Orientation change state
Left side is the current orientation.
Right side is the new orientation when the device is rotated.
Portrait ---> Landscape rotation counterclockwise rotation angle > 60
Portrait ---> Landscape_Reverse rotation clockwise rotation angle < -60
Landscape ---> Portrait rotation clockwise rotation angle < 30
Landscape ---> Portrait_Reverse rotation counterclockwise rotation angle > 150
Landscape_Reverse ---> Portrait rotation counterclockwise rotation angle > -30
Landscape_Reverse ---> Portrait_Reverse rotation clockwise rotation angle < -150

The rotation angle can not be calculated when the device is laying flat. This is why if you start an app when the device is laying flat and rotate the device no orientation change occurs.
You can see how the rotation angle is calculated at my answer at How to measure the tilt of the phone in XY plane using accelerometer in Android

Community
  • 1
  • 1
Hoan Nguyen
  • 17,479
  • 3
  • 47
  • 51
  • Please tell, where did you found this information about 60 and 30 degrees. I have been browsing android source code and couldn't find it. Also, your solution seems promising. – Twisted Mar 01 '13 at 11:44
  • I wrote an app to calculate the rotation angle and show the rotation angle in the UI. When I rotate the device, the OS changes the orientation as describe in the answer. – Hoan Nguyen Mar 01 '13 at 11:54
  • There seems to be a problem when tilting the device using two axes at the same time (it does not resemble the actual activity orientation change). Also, your orientation change state lacks the Portrait_Revers ---> Landscape && Portrait_Revers ---> Landscape_Reverse. – Twisted Mar 01 '13 at 12:52
  • What do you means by 2 axes at the same time? I can send you my app for you to test. – Hoan Nguyen Mar 01 '13 at 13:02
  • Using roll and pitch. Left top corner of your device turn down from you. Anyway, your solution seems to work but it's not perfect. Sometimes the Portrait --> Landscape has different 'turn' angle. Sometimes is 80 sometimes is 40. Depending on the other axes. – Twisted Mar 01 '13 at 13:08
  • I just play with my app again. When rotate from Landscape counterclockwise, the orientation does not change until the rotation angle is > -150 then the OS change from Landscape to Landscape_Reverse. – Hoan Nguyen Mar 01 '13 at 13:09
  • I do not understand your comment about using roll and pitch. If the device is near flat, there is no rotation angle. So whatever "your device turn down from you" may be too much down. – Hoan Nguyen Mar 01 '13 at 13:15
  • I see what you mean, probably change orientation depends also on the inclination angle. – Hoan Nguyen Mar 01 '13 at 13:17
  • Seem like if inclination is larger than 100, no orientation change. So you can check the inclination in combine with rotation. – Hoan Nguyen Mar 01 '13 at 13:22
  • Your solution is acceptable but it still needs a little more of improvement. I'll accept it as solution to my problem, but Ill have to spend more time understanding the 3d math. Thanks. – Twisted Mar 01 '13 at 13:22
0

Use getRotation()

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

from the docs

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.

Michele
  • 6,086
  • 2
  • 37
  • 44
  • Again, this is how to the the most top activity rotation not the device orientation. – Twisted Mar 01 '13 at 11:45
  • No, its the orientation of the Display / Device. You see there is Surface.ROTATION_0, Surface.ROTATION_90, Surface.ROTATION_180 and Surface.ROTATION_270 – Michele Mar 01 '13 at 14:27
  • Have you checked this on actual device? Please do not bother answering if you can't distinguish what's a surface orientation and what's a device orientation. – Twisted Mar 01 '13 at 14:52