Catching screen rotation / orientation

Peter Simpson

Expert
Licensed User
This solution is only for services, especially widgets. Service modules do not have Activity_Resume.

After looking at a few suggestions to catch screen rotation in services, I decided to use the timer that I already had running at 1000ms cycles and variables in Globals.

I just thought I would share it with bit of code with forum users.
B4X:
Sub Process_Globals
  Dim CheckScrOrientation, StoredOrientation As String
  Dim RotateTimer As Timer
End Sub

Sub Service_Start(StartingIntent As Intent)
  RotateTimer.Initialize("SDTime", 500)  '1000 Milliseconds = 1 second
  RotateTimer.Enabled = True
End Sub

Sub SDTime_Tick
  If GetDeviceLayoutValues.Width > GetDeviceLayoutValues.Height Then
      CheckScrOrientation = "Landscape"
  Else
      CheckScrOrientation = "Portrait"
  End If

  If CheckScrOrientation <> StoredOrientation Then
      StoredOrientation = CheckScrOrientation
      'PUT WHATEVER UPDATE/REFRESH ROUTINE OR CALLSUB YOU NEED TO HERE...
  End If
End Sub
This is the best solution I could come up with to catch screen rotation in services. It works for me so I'm sticking with it :)

Thank you, I hope that this helps someone one day.
 
Last edited:

dagodom

Member
Licensed User
Rotate not work

Hello
I Have 2.25 version but when rotate device activity_create and activity_resume not called.
I need to upgrade the version ?
 
Upvote 0

dagodom

Member
Licensed User
No, orientation is both

this is the sample code, may bee i wrong something...
Thank for your time

'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim relo As Boolean
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.
Dim manjobview As WebView

End Sub

Sub Activity_Create(FirstTime As Boolean)
Msgbox("create", "pippo")
'Do not forget to load the layout file created with the visual designer. For example:
'Activity.LoadLayout("Layout1")
manjobview.Initialize("manjobview")
manjobview.JavaScriptEnabled=True
manjobview.Enabled=True
If FirstTime = True Then
Activity.AddView(manjobview,0,0,100%x, 100%y)
manjobview.LoadUrl("https://www.google.com")
End If
End Sub

Sub Activity_Resume
Msgbox("resume", "pippo")
If relo = True Then
Activity.AddView(manjobview,0,0,100%x, 100%y)
manjobview.LoadUrl("https://www.google.com")
relo=False
End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
Msgbox("pause", "pippo")
End Sub



Sub activity_KeyPress (KeyCode As Int) As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then
relo = True
Select Msgbox2("Exit ?","", "Yes", "", "No",Null)
Case DialogResponse.NEGATIVE
relo = False
Return True
Case DialogResponse.POSITIVE
Return False
End Select
End If
End Sub
Sub manjobview_OverrideURL (Url As String) As Boolean

If Url.Contains("mailto:") Then
Dim Message As Email
Dim vmail As String
vmail = Url.SubString(7)
Message.To.Add(vmail)
'Message.Attachments.Add(File.Combine(File.DirRootExternal, "SomeFile.txt"))
StartActivity(Message.GetIntent)
Return True
End If
If Url.Contains("tel:") Then
Dim p As PhoneCalls
Dim vtel As String
vtel = Url.SubString(4)
StartActivity(p.Call(vtel))
Return True
End If
If Url.EndsWith(".pdf") Then
Msgbox("Catch","Pdf")
End If
Return False

End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Check the logs while you rotate the device.
** Activity (main) Create, isFirst = true **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **

Note that msgboxes and debug breakpoints in Activity_Pause are ignored (it is not possible to break at that point).
 
Upvote 0

dagodom

Member
Licensed User
I use device with wireless connection
Connect log to B4bridge, get some message and after nothing in the log when use application.
In AVD i can't rotate the device ..

Sorry, i am beginner :sign0013:
 
Upvote 0

dagodom

Member
Licensed User
When start application i have:

** Activity (main) Create, isFirst = true **
** Activity (main) Resume **

press ctrl+f11 , device rotate but in log nothing :BangHead:

Thank you
 
Upvote 0

klaus

Expert
Licensed User
When you turn the Emulator you should get something like this:

** Activity (main) Pause, UserClosed = false **
** Activity (main) Create, isFirst = false **
** Activity (main) Resume **

Could you post rproject as a zip file ( IDE menu Files / Export As Zip) so we could have a look at it.

Best regards.
 
Upvote 0

dagodom

Member
Licensed User
I think i understand the problem and my mistake
I have changed manifest file with :

SetActivityAttribute(main, android:configChanges, "keyboard|keyboardHidden|orientation|screenSize")

but this i think don't call the activity, now i remember this.
My apologize.
Thank's for your answer e sorry for lost your time.

Best regards
domenico
 
Upvote 0

Bryan

Member
Licensed User
I know this is an old topic and was addressed somewhat above. I just want make note here that I too experience the same problem with screen rotation. The text in my label disappears. This will only occur when you assign a text value to the label in code. If you use designer and put a fixed text into the label it works fine.
I am updating a label that I use for a widget to display a count down timer. So, the label's text is assigned in code not in the designer.
Again as screen orientation is changed the label gets recreated or redrawn but the text value is now gone. I am now using the timer also and it works. Maybe it's not the best way but it would be good if there was an event that detected the screen rotation in a service module for widgets.
Also, I forgot to mention that if I open any application over the top of my widget, like for instance "settings" it will at times also cause my label (in a home screen widget) to lose it's text contents when that application is closed. Any way to detect when any application is run from the home screen? I've been trying to find a way around these problems for months. I gave up for awhile (several months) and thought I would again try to figure out a solution.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Please start a new thread fopr your question instead of pulling out an old thread.
Describe your problem and in best way attach a small project which shows this problem.
 
Upvote 0

DarkAngel

Member
Licensed User
Hi there,

I have a problem with screen/device rotation. I can detect where there was a rotation of the device.
The problem is I use a compass and the angle of North's not correct when screen is in a specific position or rotation.
When I turn it upside down then angle has 180º difference, but I have to add 90º when the device is rotated 90º.
The problem is that I cannot detect in the device's exactly position/rotation.
I would need to detect if the device was rotate 90º (right) or 180º (left), and in one of this scenarios the compass indicates a wrong north.
Adding 90º or 180º to the compass angle will solve the problem only in on of the above devices positions/rotations.

How can I detect the device rotation angle? like 0º 90º 180º 270º? and compensate to the compass north calculation angle.
 
Upvote 0
Top