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

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.

thanks.
 

Arf

Well-Known Member
Licensed User
I do this, it works welll:
B4X:
Sub Process_Globals
    Dim p As Phone
End Sub
B4X:
If GetDeviceLayoutValues.Width > GetDeviceLayoutValues.Height Then
        p.SetScreenOrientation(0)    'landscape
    Else
        p.SetScreenOrientation(1)    'portrait
    End If

so it just locks the orientation to whatever it was when the activity started.
 
Upvote 0

NJDude

Expert
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:
B4X:
SetActivityAttribute(Main, android:screenOrientation, "portrait")
SetActivityAttribute(Extra, android:screenOrientation, "landscape")
 
Upvote 0

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:
Upvote 0
Top