Tistory View

반응형

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
NOTICE
RECENT ARTICLES
RECENT REPLIES
Total
Today
Yesterday
LINK
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Article Box