Tistory View
How to detect android device orientation and rotation
What should I do? 2019. 7. 29. 17:51Detect orientation
A android device has portrait and landsape orientations, usually, you could layout UI item, most thing can do by here.
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
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 devicebreak;
}
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.
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
'Android Develop > helper' 카테고리의 다른 글
안드로이드 http 다운로드하기( HttpURLConnection + SSL ) (0) | 2020.12.18 |
---|---|
안드로이드 Bitmap 로드하기 (0) | 2019.09.08 |
java int와 unsigned byte 변환 (4) | 2019.08.04 |
안드로이드 기기 방향 orientation, rotation 알아내기 (4) | 2019.07.29 |
Android ASyncTask 사용 (0) | 2019.06.26 |
- Total
- Today
- Yesterday
- 애드센스
- choreographer
- Android
- OpenGLes
- 재태크
- ComputeShader
- 티스토리
- texture
- 적금
- 예금
- 에어컨
- OpenGL ES
- 사용료
- 전기료
- 전기요금
- 안드로이드
- 텍스처
- 아끼는 법
- 컴퓨트쉐이더
- 애드핏
- 컴퓨트셰이더
- TTS
- gpgpu
- 전기세
- 재테크
- 금리
- 블로그
- 공유 컨텍스트
- 경제보복
- 에어콘
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |