Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting mirrored orientation changes in android

Normally rotating device by 90 degrees, (portrait to landscape, or reverse) causes a configuration change, Activity is destroyed and re-created etc, so it can just save value of Display.getRotation() at start and work with it.

However, when rotating device directly from 0 to 180 (portrait to portrait), or 90 to 270(landscape to landscape), no configuration change is done, device simply re-maps the screen. This makes sense since aspect ratio of layout is not changed and it need not be altered. But this makes it impossible for Activity to detect when such changes happen, i.e when Surface.ROTATION_90 went to Surface.ROTATION_270 etc.

Apart from polling Display.getRotation(), is there any better way to detect this change ?

like image 964
S.D. Avatar asked Feb 03 '13 17:02

S.D.


1 Answers

You can use the helper class OrientationEventListener.

Something like this:

public void onOrientationChanged(int orientation) {
   if (orientation == ORIENTATION_UNKNOWN) return;

   int rotation = activity.getWindowManager().getDefaultDisplay()
         .getRotation();
   int degrees = 0;
   switch (rotation) {
     case Surface.ROTATION_0: ....
     case Surface.ROTATION_90: ....
     case Surface.ROTATION_180: .....
     case Surface.ROTATION_270: .....
   }

}
like image 95
Gabriele Mariotti Avatar answered Sep 30 '22 14:09

Gabriele Mariotti