Android Question Detect Screen Rotation with Portrait Orientation

Discussion in 'Android Questions' started by Scantech, Jun 19, 2019.

  1. Scantech

    Scantech Well-Known Member Licensed User

    Can we some how determine the screen is rotated when Screen Orientation is set as Portrait only or Landscape only. I have B4X Panels in Portrait/Landscape view and i need to detect the screen orientation as it is rotated to Portrait/Landscape mode, so then i can call pnl.Rotation to rotate the panels.

    This is useful to rotate many gauges very fast, instead of rotating and loading gauges.
  2. Erel

    Erel Administrator Staff Member Licensed User

    The screen will not rotate at all if you set #SupportedOrientations to landscape or portrait. Are you setting it to something else?
  3. Scantech

    Scantech Well-Known Member Licensed User

    #SupportedOrientations is unspecified.

    I have the screen at Portrait Mode using Orientation.SetScreenOrientation. My Gauges are in Portrait view and i want to rotate them when the screen is in landscape mode. is it possible to know the screen Mode even with Orientation.SetScreenOrientation is set to portrait mode?
  4. Erel

    Erel Administrator Staff Member Licensed User

    Sorry but I don't understand. Why do you need to use Phone.SetScreenOrientation? Why do you lock the orientation if you want to rotate the layout?
  5. Scantech

    Scantech Well-Known Member Licensed User

    I don't want to rotate device and load the gauges (Don't need Create and Resume..time consuming this way). So, i choose to lock the screen with Phone.SetScreenOrientation and rotate the gauge panel which is faster method. The B4XView has a Rotation feature that i like to take advantage with. I think some of the popular OBD2 apps do it this way. The Gauges will rotate in fractions of a second instead of loading gauges(Create and Resume) which can take few seconds.

    I can only think of using a button for pressing to allow gauge panels to rotate, but i really like to know if i can find the screen angles with some sort of sensors so i may eliminate the button.
  6. sorex

    sorex Expert Licensed User

    so you want to lock the phone in portrait mode and show the data as landscape?

    why else would you want to rotate these objects?
  7. emexes

    emexes Well-Known Member Licensed User

    Perhaps something has gone missing in translation, but... it sounds like you just want to know the physical orientation of the phone. Accelerometers are the go. They're usually not particularly accurate, especially in non-high-end Android phones, but if all you need to know is whether the phone screen orientation is horizontal or vertical, they are plenty accurate enough.

    This post here works great (just tried it, still works with current phone library):

    You can skip the trig calculations, and just do something basic like:
    If accValue(1) > 7 Then
        Orientation = ORIENT_PORTRAIT
    Elseif accValue(
    0) > 7 Then
        Orientation = ORIENT_LANDSCAPE_LEFT
    Elseif accValue(
    0) < -7 Then
        Orientation = ORIENT_LANDSCAPE_RIGHT
    End if
    where 7 is close enough to gravity G = 9.8 ms-2 times Sqrt(1/2) aka Sin(45 degrees) to make the orientation changeover occur about halfway through the turn.
    Scantech likes this.
  8. emexes

    emexes Well-Known Member Licensed User

    For what it's worth, this is the sample code distilled down for detecting orientation only:
    #Region  Project Attributes 
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    'SupportedOrientations possible values: unspecified, landscape or portrait.
        #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #End Region

    #Region  Activity Attributes 
    #FullScreen: False
    #IncludeTitle: False
    #End Region

    Sub Process_Globals
    'These global variables will be declared once when the application starts.
        'These variables can be accessed from all modules.

    Private accelerometer As PhoneSensors
    Private accValues() As Float
    Dim LastOrientation As Int
    End Sub

    Sub Globals
    'These global variables will be redeclared each time the activity is created.
        'These variables can only be accessed from this module.

    End Sub

    Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    If FirstTime Then
    End If
    End Sub

    Sub Activity_Resume

    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub

    Private Sub Accelerometer_SensorChanged (Values() As Float)
        accValues = Values
    Dim X As Float = accValues(0)
    Dim Y As Float = accValues(1)
    Dim Z As Float = accValues(2)

    If False Then   
    Dim G As Float = Sqrt(X * X + Y * Y + Z * Z)
    Dim S As String = "XYZ ="
        S = S & 
    " " & NumberFormat2(accValues(0), 133False)
        S = S & 
    " " & NumberFormat2(accValues(1), 133False)
        S = S & 
    " " & NumberFormat2(accValues(2), 133False)
    End If
    Dim ThisOrientation As Int = LastOrientation
    If Y > 7 Then
            ThisOrientation = 
    else if X > 7 Then
            ThisOrientation = 
    else if X < -7 Then
            ThisOrientation = 
    End If
    If ThisOrientation <> LastOrientation Then
    Select Case ThisOrientation
    Case 4: LogColor("Orientation Landscape Left"Colors.Blue)
    Case 6: LogColor("Orientation Landscape Right"Colors.Blue)
    Case 8: LogColor("Orientation Portrait"Colors.Blue)
    End Select

            LastOrientation = ThisOrientation
    End If

    End Sub
    Scantech likes this.
  9. techknight

    techknight Well-Known Member Licensed User

    Here is a prime example on why you would want to do this:

    ocalle and Scantech like this.
  10. emexes

    emexes Well-Known Member Licensed User

    That's pretty impressive. I'd previously used a turntable to get an idea of the crosstalk between accelerometer and gyroscope readings (on an iPhone 4s), and until I saw your video I would have said that you'd need a gyroscope to actually measure rotation or orientation in the plane perpendicular to gravity.

    The video made me think a bit harder, and I've come up with two guesses at ways to measure (and one to appear to measure) rotation without a gyroscope.

    Is measuring turntable speed your actual application, though? My first guess was that you wanted your OBD app to respond quickly between orientation changes. My second guess (after the bit where I said don't worry about the trig calculations...) was a steering-wheel-mount mode for your app.
  11. Scantech

    Scantech Well-Known Member Licensed User

    Thanks emexes. Thats what i was looking for.
  12. Scantech

    Scantech Well-Known Member Licensed User


    No, but seems very interesting.
  13. Scantech

    Scantech Well-Known Member Licensed User

    emexes likes this.
  14. emexes

    emexes Well-Known Member Licensed User

    What about the steering-wheel-mount mode for your app? Is it possible to do a pass through all the widgets at the end and rotate them all according to the phone orientation so that they remain horizontal? That could be cool, not sure about being of practical advantage, though. One of those things you have to give it a go to see what it's actually like.

    I was going to suggest that a similar effect would be to move widgets slightly to the left or right due to centrifugal force as you corner, ie, when you turn to right, they bunch up towards the left. Not by much: widgets already at the far left would stay put, widgets at the far right move left say 5% of the screen width, widgets in the middle move half that.

    Then I realised that when the steering wheel was not level, that would mean you're going around a corner and thus also measuring the centrifugal force, and thus the widgets would not actually remain vertically oriented. Although... we know that G is ~9.81, so perhaps it is possible to split the nett vector into its gravity and centrifugal components.

    Anyways, all stuff for a rainy day.
  15. emexes

    emexes Well-Known Member Licensed User



    Dare I ask if you went through an intermediate stage where the rotations weren't restricted to being in 90 degree steps?

    Can you remove that restricton?

    Or only lock to the nearest 90 degrees when within +/- 15 degrees?
    Last edited: Jun 22, 2019
  16. Scantech

    Scantech Well-Known Member Licensed User

    I will be looking into it.

    Another advantage with the procedure i demonstrated is Out of Memory issue has been resolved. I may have many gauges on screen and there are times out of memory occurs when i rotate the device numerous times.

    If understand you correctly, the video does not record properly according to orientation. At the end of the video, i rotated my device to landscape mode and the widgets rotated along with it. Same with portrait mode. It does not show in the video proper orientation.

    When you say remain horizontal, are you referring to scrolling?
    Last edited: Jun 22, 2019
  17. Scantech

    Scantech Well-Known Member Licensed User

    I use 90, 0, and -90 based on Accelerometers.

    It locks based on your example you provided. Rotation is called when orientation is different then last orientation.
  18. Scantech

    Scantech Well-Known Member Licensed User

    Oh, I see what you are talking about. Currently, it rotates when angle is greater then 45 degrees. I need to figure out how to make it +- 15 degrees.
  19. emexes

    emexes Well-Known Member Licensed User

    'tis always nice when laws of nature are working with you rather than against you :)

    Yeah, I guessed that the verticalness of the video probably did not reliably/constantly match the verticalness of the device.

    I meant that, as you rotate the steering wheel, and thus the orientation angle of a phone attached to the steering wheel changes also, that the widgets on the screen would rotate (relative to the phone) so that they were always right way up (and thus readable for the driver).
  20. emexes

    emexes Well-Known Member Licensed User

    Now I've let that idea simmer for a while, I think that locking to the -90/0/90/180 degree angles when within 15 degrees of them, might be overkill.

    And I think the idea that the gauges "lean into the curve with you" as you go around corners, if they are based on the vector of both gravity and centrifugal forces... that might be rather cool.

    Given that the driver's probably leaning into the curve as well, that would mean that the gauges are oriented even better for the driver, than they would be if you just kept them level horizontal.

    I'd mock something up, but I have a massive deadline for Monday.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice