Android Code Snippet Freeze Orientation

SubName: FreezeOrientation

Description: There are times such as when processing data with a progress bar in a class, that you may want to temporarily stop the user from being able to rotate the device as it's difficult to rebuild the Gui in the middle of a process. These subroutines will get the current rotation and fix it, until it's released.

I've tested it on a Nexus 7 and it works OK. It uses getRotation which is only available from API 8 as getOrientation will return -1 (unspecified) if it is not explicitly set. This is then mapped to the required orientation.

It'll be interesting to see if these codes are consistent on other devices.

B4X:
Sub FreezeOrientation
    Dim Orient As Int
    Select GetOrientation
        Case 0
            Orient = 1            'Portrait
        Case 1
            Orient = 0            'Landscape
        Case 2
            Orient = 9            'Reverse Portrait
        Case 3
            Orient = 8            'Reverse Landscape
    End Select
    PH.SetScreenOrientation(Orient)
End Sub

Sub GetOrientation As Int
  Dim r As Reflector
  r.Target = r.GetContext
  r.Target = r.RunMethod2("getSystemService", "window", "java.lang.String")
  r.Target = r.RunMethod("getDefaultDisplay")
  Return r.RunMethod("getRotation")
End Sub

Sub ReleaseOrientation
    PH.SetScreenOrientation(-1)
End Sub

Depends on: Phone , Reflection

Tags: Freeze Orientation
 
Last edited:
Top