stoefln
stoefln

Reputation: 14595

android: How to cancel orientation change

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?

Upvotes: 1

Views: 472

Answers (4)

C0deAttack
C0deAttack

Reputation: 24667

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.

Upvotes: 2

Arslan Anwar
Arslan Anwar

Reputation: 18746

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

Upvotes: 0

Jordi
Jordi

Reputation: 1367

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)

Upvotes: 0

Diego Torres Milano
Diego Torres Milano

Reputation: 69396

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

Upvotes: 0

Related Questions