I want to do something different depending on the in which the screen is rotated for this there are four options landscape right, landscape left, portrait right, and portrait left, I know how to detect landscape and portrait but don't know how to detect between their right and left counterparts. I found this but everything is now deprecated
CodePudding user response:
You should use the onOrientationChanged event listener found in the OrientationEventListener class(https://developer.android.com/reference/android/view/OrientationEventListener) It returns an integer ranging from 0 to 359 degrees.
CodePudding user response:
defaultDisplay
was marked as deprecated in API level 30 and above. So for build version greater than 30 we have to use new solution.
Below is the code i used.
val rotation = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
display?.rotation
} else {
windowManager.defaultDisplay.rotation
}
val angle = when (rotation) {
Surface.ROTATION_90 -> 90
Surface.ROTATION_180 -> 180
Surface.ROTATION_270 -> 270
else -> 0
}