2

Gone through several posts and threads on configuration and orientation changes but still I am not cleared.

As onConfigurationChanged being called only when orientation changes from landscape to portrait or either portrait to landscape(as name suggests), but is there any way to listen reverseLandscape or reversePortrait orientation changes through any kinda listers at any api level.

Or is it possible to listen rotation changes same like onConfigurationChanged ??

Any kinda help will be highly appreciated !!

Daud Arfin
  • 2,499
  • 1
  • 18
  • 37

2 Answers2

1

You can retrieve which of the 4 orientations is currently active by calling

getWindowManager().getDefaultDisplay().getRotation();

Knowing when to call it can be tricky, as the system won't notify you if you go straight from portrait to reversePortrait for instance. The only way to achieve that is using the device's sensors. This article from the Android Developers Blog should help you.

minipif
  • 4,756
  • 3
  • 30
  • 39
1

You can use accelerometer and magnetic field sensors to calculate the device rotation and then call

getWindowManager().getDefaultDisplay().getRotation();  

when the rotation is at certain value. The way to calculate the rotation is at my answer How to measure the tilt of the phone in XY plane using accelerometer in Android

Community
  • 1
  • 1
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
  • thanks for your reply, but i don't wanna go for that much calculations to know just orientation as i thought their will be any call back listeners which will give the rotation or orientation whenever device rotates, bad luck i dint found answer yet !! +1 – Daud Arfin Mar 23 '13 at 17:48
  • Actually, you do not need to use sensors to get the rotation. Just register for OrientationEventListener. It will give you the rotation angle. – Hoan Nguyen Mar 23 '13 at 17:55
  • what i want is already mentioned in my question, reverseLandscape and reversePortrait !! – Daud Arfin Mar 23 '13 at 17:58
  • Yes, I understand. OrientationEventListener gives you the angle as you rotate the device. It will not tell you if the device is in Portrait or Landscape or whatever else. All I meant is you can use the value return by the OrientationEventListener to call getRotation when that value passed certain value, say +- 160. – Hoan Nguyen Mar 23 '13 at 18:06
  • that sounds good let me try, if it works i will accept your answer. – Daud Arfin Mar 23 '13 at 18:13
  • I implement the idea with a simple change of a textview and it works out quite well. I posted the code answer at http://stackoverflow.com/questions/15585807/android-landscape-right-to-landscape-left-event/15595770#15595770 – Hoan Nguyen Mar 24 '13 at 06:44