1

I want to cancel auto rotation of views in my app. There are a lot of posts targeting this topic[1], all of them suggesting things like setting

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

or

 <activity android:name=".myActivity"                  
          android:screenOrientation="portrait">

which does exactly what I need, but prevents onConfigurationChanged() from firing, meaning that I am not able to track the orientation of the phone any longer. The thing is: I need to know about the orientation at some point.

Any ideas how I could get the orientation of the phone without enabling rotation of views?

[1] How do I disable orientation change on Android?

Community
  • 1
  • 1
stoefln
  • 14,498
  • 18
  • 79
  • 138

4 Answers4

2

Might I suggest using the OrientationEventListener? I think you will still get the notifications even if you set the orientation in your manifest.xml file.

C0deAttack
  • 24,419
  • 18
  • 73
  • 81
0

If you need to obtain the rotation use Display#getRotation().

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • the weird thing is (very uncool!!): this only works if you don't fix the screen orientation (e.g. with setRequestedOrientation) – stoefln Jan 06 '12 at 16:21
0

If you need this information for rotating the screen, you should implement the onConfigurationChanged yourself.

If you need this information for any other purpose, I think you should use the sensors (accelerometer or compass)

Jordi
  • 1,357
  • 12
  • 20
  • I already overwrote onConfigurationChanged, but still my views are rotated, and I don't know how to intercept this orientation change. I need the orientation for saving the images with the right orientation (I'm writing a camera app) – stoefln Jan 06 '12 at 16:20
0

As you say "The thing is: I need to know about the orientation at some point."

You can check orientation any time

int orientation = getWindow().getWindowManager().getDefaultDisplay().getOrientation();
    if(orientation == 0){ //horizontal

    }else if(orientation == 1){ //vertical

    }

Orientation detail : http://developer.android.com/reference/android/R.attr.html#orientation

Arslan Anwar
  • 18,746
  • 19
  • 76
  • 105