Android Question How to freeze the orientation of an Activity - Reflection?

Similar threads

B4A Code Snippet Freeze Orientation
B4A Class ColorPalette and CircularPalette
B4A Question Classes are soon coming...
B4A Question B4A 6.3, Freezed on Designer
B4A Library [Class] Message

Gary Milne

Active Member
Licensed User
I have an app that mostly uses Landscape but has one Portrait activity.

I need to be able to set and freeze the orientation on a per activity basis.

I've tried Phone.SetScreenOrientation as described here but that does not lock the orientation and it still changes on a device rotation.

There is a thread that describes how to do this (how to lock orientation during runtime) and it seems like it would be a fit for Reflection but I don't have a good handle on Reflection semantics.

Is anyone willing to translate it for me or give me a pointer.



Well-Known Member
Licensed User
I do this, it works welll:
Sub Process_Globals
    Dim p As Phone
End Sub
If GetDeviceLayoutValues.Width > GetDeviceLayoutValues.Height Then
        p.SetScreenOrientation(0)    'landscape
        p.SetScreenOrientation(1)    'portrait
    End If
so it just locks the orientation to whatever it was when the activity started.


Licensed User
No need for reflection or phone library, you can set each activity orientation using the manifest editor, let say you have 2 activities, Main and Extra:
SetActivityAttribute(Main, android:screenOrientation, "portrait")
SetActivityAttribute(Extra, android:screenOrientation, "landscape")

Gary Milne

Active Member
Licensed User
Two great answers, thank you both very much.

I see why it did not work for me originally, the article I referenced used these constants.
' 1 = Portrait' 2 = Landscape' 9 = Reverse Portrait' 8 = Reverse Landscape
so I was doing p.SetScreenOrientation(2) to try and get landscape and that is now clearly wrong.

I used the SetActivityAttribute option and it works flawlessly.
Last edited: