Tistory View

Android Develop/helper

How to detect android device orientation and rotation

God Dangchy What should I do? 2019. 7. 29. 17:51

Detect orientation

A android device has portrait and landsape orientations, usually, you could layout UI item, most thing can do by here.



Add next code to do this, in AndroidMainfest.xml.



android
:configChanges="keyboardHidden|orientation"



if android:configChanges already exists, only append orientation with "|"



Override onConfigurationChanged function in your activity.



public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

if( newConfig.orientation == ORIENTATION_LANDSCAPE ) {
    // landscape
}
else {
    // portrait
}
}



Enter code to do by orientation.




Run on firsrt start

But, this onConfigurationChanged function is not call on creating activity, so, need more job to do this in onCreate function.

Resources r = Resources.getSystem();
Configuration config = r.getConfiguration();
if( config.orientation == ORIENTATION_LANDSCAPE ) {
    // landscape
}
else {
    // portrait
}








Detect rotation of the device

But, I need orientation of device that is on left landscape or right landscape. Next code bring me to do this.

Display display = (YourActivity).getWindowManager().getDefaultDisplay();


float newRotation = 0.0f;

switch( display.getRotation() )
{
case Surface.ROTATION_0:
// portrait on hand-held device
break;
case Surface.ROTATION_90:
// left landscape on hand-held device
break;
case Surface.ROTATION_180:
// most hand-held devices do not have flip rotation
break;
case Surface.ROTATION_270:
// right landscape on hand-held device

break;
}

When There is this code in onConfigurationChanged, Other problem happened, If users rotate their devices from left landscape to right landscape quickly, onConfigurationChanged is not called.




Fortunatly, Android support other method from API17(JellyBean MR1)



DisplayListener of DisplayManager

We can detect rotation of device if registering DisplayListener interface of DisplayManager.


somewhere in onCreate function

if(Build.VERSION.SDK_INT >= 17 ) {
DisplayManager.DisplayListener mDisplayListener = new DisplayManager.DisplayListener() {
@Override
public void onDisplayAdded(int displayId) {

}

@Override
public void onDisplayChanged(int displayId) {

Display display = YourActivity.this.getWindowManager().getDefaultDisplay();


// place operation code in case statement
switch( display.getRotation() )
{
case Surface.ROTATION_0:

break;
case Surface.ROTATION_90:
break;
case Surface.ROTATION_180:
break;
case Surface.ROTATION_270:
break;
}


}

@Override
public void onDisplayRemoved(int displayId) {

}
};
DisplayManager displayManager = (DisplayManager) this.getSystemService(Context.DISPLAY_SERVICE);
displayManager.registerDisplayListener(mDisplayListener, new Handler());
}



Because onDisplayChanged function is called severally with same rotation, To prevent duplicated operation, you must save previous value to do once.



In example code, mDisplayListener is declared as local variable, that must be a member variable of Your activity or global variable.




Unregstering Listener

the function name is start with "register", this means that you need unregister listener after use. onDestroy function is appropriate place.


DisplayManager displayManager = (DisplayManager) this.getSystemService(Context.DISPLAY_SERVICE);
displayManager.unregisterDisplayListener( mDisplayListener );




Other note.

Activty can be Listener for easy to use.

I did not test this on tablets, on tablets, basic landscape mode has zero degree, and listener called every direction, event if them flipped





Replies
Reply Write