Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Detect Orientation Changed

Tags:

I need to detect an orientation change in my application, but I don't want my layout to be changed from portrait to landscape. Currently I'm using the OrientationEventListener however detecting the orientation angle is not enough. I want to detect that the user changed from portrait to landscape or viceversa, and that is not just detecting if the orientation angle is 90 or 270.

I want to do the same detection that the Android does to change the activity's orientantion. I tried overriding onConfigurationChanged and check if orientantion is landscape/portrait, however this still changes my activity layout to landscape.

Is there a way to use onConfigurationChanged but force the layout to stay in portrait?
Is there another way to detect orientantion change without using OrientationEventListener. Ultimately I can implement my own orientation changed algorithm, any ideas on this? It has to be something more complex than if(90-THRESHOLD <= orientation <= 90+THRESHOLD), I want to detect if the user made the complete movement Portrait->Landscape or Landscape->Portrait.

Thanks for the help,
Filipe

like image 691
ffleandro Avatar asked Nov 23 '11 19:11

ffleandro


People also ask

How do you know if orientation changes?

This example demonstrates how do I detect orientation change in layout in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How can we detect the orientation of the screen in Android?

int orientation = display. getOrientation(); Check orientation as your way and use this to change orientation: setRequestedOrientation (ActivityInfo.

What happen to the Android application when the device screen orientation has been changed from portrait to landscape view?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.

How do I know my device is fluttering orientation?

How to Detect Orientation Change in Layout In Flutter ?? In order to determine the Orientation of the screen, we can use the OrientationBuilder Widget. The OrientationBuilder will determine the current Orientation and rebuild when the Orientation changes.


1 Answers

Ok, after trying to use the Android API and not being able to do what I need, I implemented my own algorithm and actually it wasn't that complicated: I used a OrientationEventListener, and calculated if the orientation is in the 4 orientation points (in my code I only detect LANDSCAPE_RIGHT and PORTRAIT_UP:

orientationListener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_UI) {         public void onOrientationChanged(int orientation) {             if(canShow(orientation)){                 show();             } else if(canDismiss(orientation)){                 dismiss();             }         }     };  @Override public void onResume(){     super.onResume();     orientationListener.enable(); }  @Override public void onPause(){     super.onPause();     orientationListener.disable(); }  private boolean isLandscape(int orientation){         return orientation >= (90 - THRESHOLD) && orientation <= (90 + THRESHOLD);     }  private boolean isPortrait(int orientation){     return (orientation >= (360 - THRESHOLD) && orientation <= 360) || (orientation >= 0 && orientation <= THRESHOLD); }  public boolean canShow(int orientation){     return !visible && isLandscape(orientation); }  public boolean canDismiss(int orientation){     return visible && !dismissing && isPortrait(orientation); } 
like image 98
ffleandro Avatar answered Oct 12 '22 00:10

ffleandro