Skip to content

Instantly share code, notes, and snippets.

@ookami-kb
Created June 27, 2013 11:28
Show Gist options
  • Save ookami-kb/5875752 to your computer and use it in GitHub Desktop.
Save ookami-kb/5875752 to your computer and use it in GitHub Desktop.
How to listen to screen orientation changes when orientation is locked.
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