Getting Portrait mode's height and width when the orientation is Landscape in OrientationEventListener in Android

71 Views Asked by At

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: enter image description here

How to fix this?

2

There are 2 best solutions below

1
404NotFound On

You have mistakenly assigned width to Height function. You have written this:

width = TWOSScreenAspectsAndroid.GetScreenHeight ()

It should be:

height = TWOSScreenAspectsAndroid.GetScreenHeight ()

and vice versa

0
404NotFound On

I tried this inside MainActivity:

    private var orientationListener: OrientationEventListener? = null
    lateinit private var context: Context

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        context = this
        orientationListener = object : OrientationEventListener(this, SensorManager.SENSOR_DELAY_UI) {
            override fun onOrientationChanged(orientation: Int) {
                Log.d("orientation", "$orientation")
                if (orientation == 0 || orientation == 180) {
                    Log.d("orientation", "Orientation: Portrait")
                    getScreenHeight(context)
                    orientationListener?.disable()
                } else if (orientation == 90 || orientation == 270) {
                    Log.d("orientation", "Orientation: LandScape")
                    getScreenHeight(context)
                    orientationListener?.disable()
                }
            }
        }
        if (orientationListener?.canDetectOrientation() == true) {
            orientationListener?.enable()
        }
    }

    private fun getScreenHeight(context: Context){
        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 width: Int
        val insetsHeight: Int
        val insetsWidth: Int
        val activity: Activity
        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
            insetsWidth = insets.left + insets.right
            Log.d ("orientation", "${insets.right}, ${insets.left}, ${insets.top}, ${insets.bottom}")

            height = metric.bounds.height () - insetsHeight
            width = metric.bounds.width() - insetsWidth

        } else {

            // Code for API level 29 and lower

            displayMetrics = DisplayMetrics ()
            activity.windowManager.defaultDisplay.getMetrics (displayMetrics)

            height = displayMetrics.heightPixels
            width = displayMetrics.widthPixels
        }

        Log.d ("orientation", "Inside TWOSScreenAspectsAndroid::GetScreenHeight Height: $height $width")
    }

This is working fine for me, what I found was that whenever we rotate device application will recreate the MainActivity and it will register the orientationListener again. So i was getting multiple times onOrientationChanged and each time it was giving me wrong value except the last time. Hence i added this to get fresh value everytime.

orientationListener?.disable()

Hope this helps!!