Android Code Snippet Freeze Orientation

stevel05

Expert
Licensed User
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:

stevel05

Expert
Licensed User
Could you try just running the GetOrientation sub on the asus and see what it returns for each orientation?
 

Yalçın Kondur

Member
Licensed User
When I wait for each rotation, it returns expected results, 0 for landscape, 1 for portrait, 2 for reverse landscape and 3 for reverse portrait but a quick rotation from portrait to reverse portrait gives 1 not 3.
 

MikeH

Well-Known Member
Licensed User
Doesn`t work with Sony Xperia Z1, Samsung Galaxy Tab2 GT-P5100 and Asus Eee Pad Transformer TF101

Confirmed working with Nexus 7 2012, Nexus 7 2013 and ZTE Blade III.
 
Last edited:

stevel05

Expert
Licensed User
Thanks for he feedback guys, sounds like once again hardware vendors have slightly different implementations, or bugs.
 

luisftv

Member
Licensed User
Ok, so, if ad add a radio button or a check box in the visual designer and I call it "FreezeRadioButton" (or whatever), then how would it look in your code?

Thanks.
 

stevel05

Expert
Licensed User
Good question, it's a Phone Object Variable from the Phone Library.
B4X:
Dim PH As Phone
 

derez

Expert
Licensed User
Unbeliveable !
In some devices if I put the value that the device reads (by GetOrientation) in the
PH.SetScreenOrientation(GetOrientation) it freezes the orientation, while in others the read value is the opposite of what it needs to keeps the orientation (like in the code above).
B4X:
Select GetOrientation
        Case 0
            Orient = 1            'Portrait
        Case 1
            Orient = 0            'Landscape
Specifically - Asus tf101 works in the first way, Nexus 5 in the second.
Can someone explain it ?
 

derez

Expert
Licensed User
I tried a workaround and created this code.
Please try and report problems.
B4X:
Sub FreezeOrientation
    Dim k,m, n As Int
    k = GetOrientation
    If 100%x > 100%y Then m = 0 Else m = 1
    n = k + m
    If n Mod(2) = 0 Then
         ph.SetScreenOrientation(k)
    Else
        ph.SetScreenOrientation(1 - k Mod(2))
    End If
End Sub
It will not freeze the opposite modes but will answer for the problem of layout.
 
Last edited:

merlin2049er

Well-Known Member
Licensed User
I tried both methods of freezing the orientation. When it returns from the color palette it goes to portrait.
Isn't there a way to keep the activity in landscape?
 

stevel05

Expert
Licensed User
Hi Merlin,

This is an experiment and unfortunately, dependent on the manufactures implementation. Perhaps if you can post some example code and state what device you are using, the issue can be looked at.
 

derez

Expert
Licensed User
@JohnD:
Prevents orientation from flipping the wrong way first.
ph.SetScreenOrientation(1 + k Mod(2))
This is wrong because when kmod(2) is 1 you get 2 and it does not freeze most of the devices.
I think the best is to stick to the emulator's behaviour.
Which device works for you with this ?
 
Top