Programatically allow or disallow the orientation mode (portrait/landscape)

theos

Member
Licensed User
Longtime User
Dear All
I have an application composed by several panels, each panel should
be always visualized in portrait mode.
For this purpose, I use the "#SupportedOrientations: portrait" directive.
Only in one panel, I have an HorizontalScrollView.
When the user clicks on an image inside the HorizontalScrollView (and
only in this case), I need to switch to Landscape mode, with the selected
image painted on a TouchImageView in landscape mode.
The question is:
Is possible to catch the orientation changing event, even if
#SupportedOrientations is set to portrait, and (after some checks)
optionally change the device orientation?
Thanks
 

theos

Member
Licensed User
Longtime User
ok, do I need to use accelerometers to detect when the device is rotated or is there a way to set up an event that is triggered when orientation changes?
thank you
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
If you set the orientation to portrait or landscape there is no OrientationChanged event.
When you select an image you should set the orientation to landscape.
Phone library, Phone object SetOrientation(0).
When you close the image display you should set portrait again SetOrientation(1).

Best regards.
 
  • Like
Reactions: eps
Upvote 0

IanMc

Well-Known Member
Licensed User
Longtime User
Does this work for all devices? specifically tablets that don't have phone functions?

Thanks
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
I am trying to understand how to switch between Landscape and Portrait. I am calling "Orient.StartListening("Orient")" from Sub Activity_Resume. From the LOG tab I can see that it definately gets into "Sub Orient_OrientationChanged" but if I turn my tablet from Landscape to Portrait the UI does not follow suite i.e it stays in Landscape. Any suggestions about how to fix this problem? Thanks

B4X:
Sub Orient_OrientationChanged (Azimuth As Float, Pitch As Float, Roll As Float) 
 
    If Activity.Width > Activity.Height Then
      ph.SetScreenOrientation(0)
      Log("hello")
    Else
      ph.SetScreenOrientation(1)
      Log("goodbye")
    End If
   
End Sub
 
Upvote 0

eps

Expert
Licensed User
Longtime User
I am trying to understand how to switch between Landscape and Portrait. I am calling "Orient.StartListening("Orient")" from Sub Activity_Resume. From the LOG tab I can see that it definately gets into "Sub Orient_OrientationChanged" but if I turn my tablet from Landscape to Portrait the UI does not follow suite i.e it stays in Landscape. Any suggestions about how to fix this problem? Thanks

B4X:
Sub Orient_OrientationChanged (Azimuth As Float, Pitch As Float, Roll As Float)

    If Activity.Width > Activity.Height Then
      ph.SetScreenOrientation(0)
      Log("hello")
    Else
      ph.SetScreenOrientation(1)
      Log("goodbye")
    End If
  
End Sub

Johan, ideally you should start a new thread.. How have you defined your layouts in your App? If you've only defined portrait ones in the designer, for instance then you need to define landscape ones as well, otherwise there's nothing to display when the orientation switches. You also need to look and see if you've disabled landscape orientations, by only allowing portrait orientations, this is done in the definitions in the top of the editor in B4A.

You don't need to handle or code the screen switch, it will happen by itself, as long as you've defined something for it to show and haven't disallowed the other orientation. The code will execute the resume part and effectively the App restarts itself on orientation switches.
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Thanks for the info. I have in the mean time changed it to the code below and now it sees to be working fine - except for the last line of code within the SUB. If I start the app and hold the tablet in "normal" landscape or and of the two portrait modes the UI adjusts 100% to orientation required. Also when turning the tablet from "normal" landscape to any of the two portrait modes and back to "normal" landscape. If I however turn the tablet from "normal" landscape through 180 degrees the UI stays the same i.e now reading upside down. Is the last line of code a valid instruction with 8 being passed as the parameter?
B4X:
Sub Orient_AccelerometerChanged (X As Float, Y As Float, Z As Float)
   
    If X < 5 AND  Y > 5 Then ph.SetScreenOrientation(0)  'landscape
    If X < -5 AND Y > -5 Then ph.SetScreenOrientation(1) 'portrait
    If X > 5 AND Y < 5 Then ph.SetScreenOrientation(9)  'reverse portrait
    If X > -5 AND Y < -5 Then ph.SetScreenOrientation(8)
 
End Sub
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
Are there any good "educational" examples of screen rotation that you can refer me to? Something that generates a very smooth rotation from any one of the 4 possible screen orientations to and of the other? Mine "blinks" as it cycles through activity_pause, activity_create, activity_resume (not the order as it happens but nevertheless - it blinks on/off)
 
Upvote 0
Top