I want to get the screen height and width when the device's orientation changes and I'm using OrientationEventListener instead of the onConfigurationChanged() method.
For the below Code:
1)OrientationEventListener
mOrientationListener = object : OrientationEventListener (context, SensorManager.SENSOR_DELAY_UI)
{
override fun onOrientationChanged (orientation: Int)
{
if (gTemp == false) {
gTemp = true
} else if (orientation == 0 || orientation == 180) {
Log.d ("orientation", "Orientation: Portrait")
height = TWOSScreenAspectsAndroid.GetScreenHeight ()
width = TWOSScreenAspectsAndroid.GetScreenWidth ()
// call cpp function
OrientationChanged (pInstructionSetIndex, width, height)
} else if (orientation == 90 || orientation == 270) {
Log.d ("orientation", "Orientation: LandScape")
width = TWOSScreenAspectsAndroid.GetScreenHeight ()
height = TWOSScreenAspectsAndroid.GetScreenWidth ()
// call cpp function
OrientationChanged (pInstructionSetIndex, width, height)
}
}
}
if (mOrientationListener.canDetectOrientation ()) {
mOrientationListener.enable ()
}
2)TWOSScreenAspectsAndroid.GetScreenHeight ()
@JvmStatic
public fun GetScreenHeight (): Int
{
val metric: WindowMetrics
val insets: Insets // An Insets instance holds four integer offsets which describe changes to the four edges of a Rectangle.
val displayMetrics: DisplayMetrics
val height: Int
val insetsHeight: Int
val context: Context?
val activity: Activity
context = TWProcessStates.GetMainActivityContext ()
activity = context as Activity
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// Code for API level 30 and higher
metric = activity.windowManager.currentWindowMetrics
insets = activity.windowManager.currentWindowMetrics.windowInsets.getInsetsIgnoringVisibility (WindowInsets.Type.navigationBars() or WindowInsets.Type.displayCutout ())
insetsHeight = insets.top + insets.bottom
Log.d ("orientation_height", "${insets.right}, ${insets.left}, ${insets.top}, ${insets.bottom}")
height = metric.bounds.height () - insetsHeight
} else {
// Code for API level 29 and lower
displayMetrics = DisplayMetrics ()
activity.windowManager.defaultDisplay.getMetrics (displayMetrics)
height = displayMetrics.heightPixels
}
Log.d ("orientation_height", "Inside TWOSScreenAspectsAndroid::GetScreenHeight Height: $height")
return height
}
I'm getting Portrait mode's height and width when the orientation is Landscape and vice versa, as shown in the below logs:

How to fix this?
You have mistakenly assigned width to Height function. You have written this:
It should be:
and vice versa