Programatically Disable Screen Rotation

cbanks

Active Member
Licensed User
Longtime User
What is the code to disable rotating an app when a user changes their device orientation from vertical to horizonal or vice versa? I want my app to be able to rotate the app by default, but I want users to be able to select a setting that will disable rotating the app if the device orientation changes.
 

ericvanderhoeven

Member
Licensed User
Longtime User
My app keeps resuming [and therefore restarts]

Look at the Phone library, SetScreenOrientation.

Best regards.

Guys, this works great. I compile the program to work in portrait mode only but when I play a video, it is nicer to have it played in landscape mode. I just want to play the video in landscape and then return to portrait. I thought the above trick would not ''disturb'' my app but for reasons found elsewhere, it still kicks in activity_resume and that.... restarts the program. I can read at several places in the forum that it does this [when orientation changes], but I do not read a quick fix to stop this resuming... :BangHead:

I have also looked into StateManager but that also does not seem to solve it... Any one that can tell me how I can change the orientation of my device temporarily [for this video play] and then switch it back, without this resuming kicking and and/or a lot of tricks?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You should have given some more explanations on what you are doing.
The best solution would be to have the main program in one Activity set to portrait only and play the video in a second Activity set to landscape only. So when you lounch the video it's automatically in landscape and when you go back to the main program it's automatically in portrait.

Best regards
 
Upvote 0

shaxboz

Member
Licensed User
Longtime User
Hi!
when my app resuming after the phone sleep mode my app is restarting and screen orientation also changed to portrait.
I am using to change screen orientation ro landscape:
Dim p As Phone
p.SetScreenOrientation(0)
What should I do to pause my app when I turn off my phone screen(sleep mode) and to resume (not restart) after the sleep mode?
I am wrting this code in activity_resume and activity_create...
Dim p As Phone
p.SetScreenOrientation(0)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Do you want a specific activity always to be shown in landscape for example?
Use the manifesteditor and insert a line for each activity. Replace "activity" by the name of the activity. main for example
SetActivityAttribute(activity,android:screenOrientation, "sensorLandscape")

B4X:
Dim p As Phone
p.SetScreenOrientation(0)

You do not need to use these when changing the manifest in this way.

NOTE: You and the user cannot change the orientation to portrait in this case
 
Upvote 0

shaxboz

Member
Licensed User
Longtime User
Thanks, but why my app everytime restarting after the resume from sleep mode? What should I do to normal pause and resume?
 
Upvote 0

shaxboz

Member
Licensed User
Longtime User
ok, I will try to explain
1. I started my app (activity_create is worked). I created button and button.top=100, button.left=100
1a. some changes with objects: button.left=150, button.top=200
2. I turn off my phone screen (physically, not programmatically)
3. I turn on my screen (activity_create is restarted, not worked activity_resume). last time before the turning off button left=150 but now it is 100
I think you understand me

I wnat to my object's settings is not changed when I turn off my phone screen and resume.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…