Reputation: 109
I am trying to make methods about the display rotation, I know that the following code windowManager.defaultDisplay.orientation
is deprecated from Java to Kotlin. My goal here is to recreate as recommended to me in this website. I don't know if I done it correctly so that's why I am typing it here to be sure if I've done something wrong to edit it out or fixing it on the right order. Because I am really confused up at this point. Thank you in advance.
/**
* This will get the display ID as an Int to identify the rotation of the display, before that it casted the DisplayManager in order to get the services. Then returns to get the ID Int from the display.
*/
private fun getDisplay(displayId: Int): Display? {
val displayManager: DisplayManager = baseContext.getSystemService(DISPLAY_SERVICE) as DisplayManager
return displayManager.getDisplay(displayId)
}
/**
* This is where we get the orientation as type Int, the return statement must be Int and it's not declared as null, it needs to be non-null.
*/
fun getOrientation(): Int {
val rotation = this.getOrientation()
val orientation = when (getDisplay(rotation)!!.rotation) {
Surface.ROTATION_0 -> ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
Surface.ROTATION_180 -> ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
Surface.ROTATION_270 -> ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
Surface.ROTATION_90 -> ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
else -> Log.e("Display", "Orientation not connected.")
}
return orientation
}
Upvotes: 1
Views: 1005
Reputation: 146
If you looking for the orientation of the device wether on PORTRAIT
or LANDSCAPE
mode you can use this following code for more simplicity
private fun isPortrait(): Boolean {
val config: Configuration = resources.configuration
return config.orientation == Configuration.ORIENTATION_PORTRAIT
}
Upvotes: 2