Prevent screen from rotating

Dear App Invertors,

Is there an easy way to prevent the screen from rotating? As I'm scaling all visible components depending on screen height and width, there is a problem when the screen rotates. I need to set and lock the screen orientation.

Please advise, thanks!

Set the Screen1 Orientation attribute in the Designer, to Portrait or Landscape.

2 Likes

Thank you, but this is exactly what I have done and it causes the problem.

My scaling of components is connected to screen width and height. When I set the orientation in Designer to Portrait and when the device is in Landscape, the scaling is done based on height and width of the Landscape, then it turns the display to what I have set (=Portrait) and the scaling is wrong due to being calculated and set in Landscape.

So: How can I prevent the device from turning the screen at all? It must not turn at all, it must be locked in Portrait and never any else orientation.

How could this be achieved?

Thank you

Just the way @ABG told you.

I'm afraid you missed something or didn't entirely read what I wrote. I wrote that I did it exactly this way. But my screen still rotates (screen rotating enabled in UI) despite my hard specifying of the screen orientation in Designer

1 Like

Perhaps your device precludes that setting because of a device System setting set to auto rotate?

See Android rotate screen system setting - Google Search

Check your device's System settings and see if you can turn off the device's system auto rotate; then test using Portrait or Landscape and tell us what you discover. Thanks.

1 Like

Thank you, now we come closer to the original question, which I should have defined more precisely:

How can I disable system screen rotation with the help of AI2 (so inside my app)?

It's working to me, despict the fact that device "Rotate ON"

Also tested on Android 10 and 12 with Auto-rotate set in device settings, when AI2 app set to portrait, the screen stays where it is, in portrait.

Well, maybe this is related to a specific combination of Android version, UI and AI2.

But for me, it is not working, why should I post if there was no issue.. :roll_eyes:

I hold my phone in Landscape and Designer is set to Portrait -> Screen rotates to Landscape and back to Portrait

1 Like

..I can send a screen recording if you can't believe me..

I have an idea what could be the reason for this:

My app is really huge, and it needs some time to load a screen.

Maybe you can just not see that the screen is rotated back and fourth, as this is performed so fast with a simple or small app

Screen recording is probably useless. You are seeing something others do not see; possibly because of how you wrote your code.

Share your aia (or a small test example that exhibits the same behavior) to allow someone to test your code on their devices and see if they can replicate your issue.

The problem might exist because your project is huge as you said. No one can provide specific advice without access to an example that fails as you say.

This might be the case as you might be already executing a event-response, once that get done, app will be able to response to the new one, that is screen rotation.

There is no orientation switching/resetting, it just stays in portrait (tested with a large app).
(also tested with compiled app)

As suggested, provide a small example aia we can test.

Also, what device and android version are you testing with ?

OK thank you for your efforts!

I can't provide a small aia., I would first have to determine how I could extract the relevant parts of my app. It is really complex with ~25.000 blocks in total.

I am testing with my Redmi 9 and Android 11. My wife has a Galaxy A51 and it's the same problem..

Must be my error from what you have stated here. But I searched for it for many many hours and I couldn't find it. My workaround is to disable system screen rotation, but other users of my app don't know about this, of course..

Just try with any other aia project. Does the same thing happen, or does portrait stay portrait. This would tell us whether it is something in your app or an external issue.

Java developers probably use an intent to do that ( intent disable auto screen rotation - Google Search ).
Intents are available in App Inventor by using the ActivityStarter to access the System settings and manually make the change. Will this work for you? No idea.

1 Like

How much work happens in your global init blocks in Screen1?

How about in Screen1.Initialize?

Is it possible to defer some startup work until after the screen has been layed out for display, perhaps by moving the work into a one shot Clock Timer?

How are your Screen1 component Height and Width attributes set?
Automatic Height and Width take longer to compute because they depend on content size, versus Percentage, which needs only Screen1.Height and Width.

I will do that, thank you for your suggestions!

Could potentially be a solution, will test it! Thanks!

Tried that, but it results in various other timing issues then. In some of the functions, I use a one-shot clock to delay hungry procedures, so that other procedures can be processed at all. I was surprised of this behaviour, thought that procedures are executed parallel but didn't expect that behaviour of "blocking".

Tried to implement it as flexible as possible, so that the App looks the same on nearly any device. I did it with percentage and auto, but I also implemented my own scaling routine that scales font sizes and sizes of buttons etc. This scaling is based on my device's layout; it scales the elements with
factor = myDevice.height / thisDevice.height