iOS Question Detect orientation

Nickelgrass

Active Member
Licensed User
Longtime User
Hello,
I am making an app that can run in landscape and portrait mode and may be rotated during runtime. In my code I am setting imageviews bitmaps with LoadBitmap corresponding to statuses. According to the orientation, this image must load a rotated image or not. But how can I now distinguish the orientation?

Code:
            If i = types.STATE_CONNECTED Then
                panta_logo.Bitmap = LoadBitmap(File.DirAssets, "logo_green.png")
            else If i = types.STATE_ERROR Then
                panta_logo.Bitmap = LoadBitmap(File.DirAssets, "logo_red.png")
            Else
                panta_logo.Bitmap = LoadBitmap(File.DirAssets, "logo_yellow.png")
            End If

Thanks

edit: figured it out. it is pg.RootPanel.Width and height. But is there any event risen when orientation changes?

edit: figured it out. Its the Sub pg_Resize(Width As Int, Height As Int) event that does not appear when I enter the tab page menu. Is it supposed to not show up?
 

Attachments

  • page.png
    page.png
    4.3 KB · Views: 257
Last edited:

b4x-de

Active Member
Licensed User
Longtime User
B4X:
' Returns 1 = Portrait, 2 = PortraitUpsideDown, 3 = LandscapeLeft, 4 = LandscapeRight or -1 = Error
Private Sub getOrientation() As Int
   
    Dim no As NativeObject

    Try
        no = no.Initialize("UIDevice").RunMethod("currentDevice", Null)
        Dim orientation As Int = no.RunMethod("orientation", Null).AsNumber
        Return orientation
    Catch
        Log("Failed to get Device Orientation from UIDevice.currentDevice().")
    End Try
   
    Return -1
End Sub

Public Sub isLandscape() As Boolean
    Dim intOrientation As Int

    ' 1 = Portrait, 2 = PortraitUpsideDown, 3 = LandscapeLeft, 4 = LandscapeRight, -1 = Error
    intOrientation = getOrientation
   
    Return intOrientation = 3 Or intOrientation = 4
End Sub

Public Sub isPortrait() As Boolean
    Dim orientation As Int

    ' 1 = Portrait, 2 = PortraitUpsideDown, 3 = LandscapeLeft, 4 = LandscapeRight, -1 = Error
    ' Return true in case of error
    orientation = getOrientation
    Return orientation = 1 Or orientation = 2 Or orientation = -1
End Sub
 
Upvote 0
Top