Created
June 27, 2013 11:28
-
-
Save ookami-kb/5875752 to your computer and use it in GitHub Desktop.
How to listen to screen orientation changes when orientation is locked.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyActivity extends Activity { | |
private OrientationEventListener orientationEventListener; | |
private boolean isPortrait; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
orientationEventListener = new OrientationEventListener(this) { | |
@Override | |
public void onOrientationChanged(int orientation) { | |
if (!isPortrait && ( | |
orientation < 30 || orientation > 330 || (orientation > 150 && orientation < 210))) { | |
// Orientation changed to portrait | |
isPortrait = true; | |
} else if (isPortrait && ( | |
(orientation > 60 && orientation < 120) || (orientation > 240 && orientation < 300))) { | |
// Orientation changed to landscape | |
isPortrait = false; | |
} | |
} | |
}; | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
orientationEventListener.enable(); | |
} | |
@Override | |
protected void onPause() { | |
orientationEventListener.disable(); | |
super.onPause(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment