Android Question Open a page in specific orientation

stp

Active Member
Licensed User
Longtime User
Have B4XPage, specifig B4XPage5.bas and i want to open that page in landscape mode.
B4XPage5.bas is Type=Class. How can i do that ?
I dont want to change the orientation of that page. That page will open in landscape and stay in landscape.
 
Last edited:
1.see here and there
Before we start:

1. You are not forced to use B4XPages. All the current features behave exactly as before.
2. It does have some limitations. One notable limitation is that in B4A, the activity that holds all the pages should be locked to a single orientation.
3. It is supported by the latest versions of B4J, B4i and B4A.
 
Upvote 0
Then you have to open a second activity and set it for landscape.
see an example of use: https://www.b4x.com/android/forum/threads/exoplayer-full-screen-feature.132854/
That is exactly what i want to do. But exactly it that example in Fullscreen module:
B4X: Copy
Private Sub SwitchTargetPlayerView(FullScreenIsDestination As Boolean)
    Dim mp As B4XMainPage = B4XPages.MainPage
    Dim jo As JavaObject
    Dim player As Object = mp.Player.As(JavaObject).GetField("player")
    jo.InitializeStatic("androidx.media3.ui.PlayerView")
    jo.RunMethod("switchTargetView", Array(player, IIf(FullScreenIsDestination, mp.SimpleExoPlayerView1, SimpleExoPlayerView1), IIf(FullScreenIsDestination, SimpleExoPlayerView1, mp.SimpleExoPlayerView1)))
End Sub

i dont understand the:
B4X: Copy
  Dim mp As B4XMainPage = B4XPages.MainPage
 
Upvote 0
B4Xpages.MainPage is the class instance where the player is housed.
mp is just a synonym for that instance.
My page is named B4xPage4tv.bas on that page is my exoplayer.
In the example the player is on B4xMainpage
the code is now
B4X: Copy
Private Sub SwitchTargetPlayerView(FullScreenIsDestination As Boolean)
    Dim mp As B4XMainPage = B4XPages.MainPage
    Dim jo As JavaObject
    Dim player As Object = mp.Player.As(JavaObject).GetField("player")
    jo.InitializeStatic("androidx.media3.ui.PlayerView")
    jo.RunMethod("switchTargetView", Array(player, IIf(FullScreenIsDestination, mp.SimpleExoPlayerView1, SimpleExoPlayerView1), IIf(FullScreenIsDestination, SimpleExoPlayerView1, mp.SimpleExoPlayerView1)))
End Sub
i get the error:
B4X: Copy
Error occurred on line: 32 (FullScreen)
java.lang.RuntimeException: Object should first be initialized (SimpleExoPlayerView).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:67)
    at com.theEyeApp.fullscreen._switchtargetplayerview(fullscreen.java:412)
    at com.theEyeApp.fullscreen._activity_create(fullscreen.java:384)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:157)
    at com.theEyeApp.fullscreen.afterFirstLayout(fullscreen.java:105)
    at com.theEyeApp.fullscreen.access$000(fullscreen.java:17)
    at com.theEyeApp.fullscreen$WaitForLayout.run(fullscreen.java:83)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:210)
    at android.os.Looper.loop(Looper.java:299)
    at android.app.ActivityThread.main(ActivityThread.java:8319)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1038)
** Activity (fullscreen) Resume **
line 32 is:
B4X: Copy
jo.RunMethod("switchTargetView", Array(player, IIf(FullScreenIsDestination, mp.SimpleExoPlayerView1, SimpleExoPlayerView1), IIf(FullScreenIsDestination, SimpleExoPlayerView1, mp.SimpleExoPlayerView1)))
what is wrong ?
 
Upvote 0
My page is named B4xPage4tv.bas on that page is my exoplayer.
So you'll need to point to that page.
For example, if you made it public on the B4XMainPage, which is recommended:
B4X: Copy
Private Sub SwitchTargetPlayerView(FullScreenIsDestination As Boolean)
    Dim jo As JavaObject
    Dim player As Object = B4XPages.MainPage.B4xPage4tv.Player.As(JavaObject).GetField("player")
    jo.InitializeStatic("androidx.media3.ui.PlayerView")
    jo.RunMethod("switchTargetView", Array(player, IIf(FullScreenIsDestination, mp.SimpleExoPlayerView1, SimpleExoPlayerView1), IIf(FullScreenIsDestination, SimpleExoPlayerView1, mp.SimpleExoPlayerView1)))
End Sub
 
Upvote 0
So you'll need to point to that page.
For example, if you made it public on the B4XMainPage, which is recommended:
B4X: Copy
Private Sub SwitchTargetPlayerView(FullScreenIsDestination As Boolean)
    Dim jo As JavaObject
    Dim player As Object = B4XPages.MainPage.B4xPage4tv.Player.As(JavaObject).GetField("player")
    jo.InitializeStatic("androidx.media3.ui.PlayerView")
    jo.RunMethod("switchTargetView", Array(player, IIf(FullScreenIsDestination, mp.SimpleExoPlayerView1, SimpleExoPlayerView1), IIf(FullScreenIsDestination, SimpleExoPlayerView1, mp.SimpleExoPlayerView1)))
End Sub
dim mp ? There is no class ? exactly that is my problem
B4X: Copy
Private Sub SwitchTargetPlayerView(FullScreenIsDestination As Boolean)
    Dim mp As B4XMainPage = B4XPages.MainPage
    Dim jo As JavaObject
    Dim player As Object = mp.Player.As(JavaObject).GetField("player")
    jo.InitializeStatic("androidx.media3.ui.PlayerView")
    jo.RunMethod("switchTargetView", Array(player, IIf(FullScreenIsDestination, mp.SimpleExoPlayerView1, SimpleExoPlayerView1), IIf(FullScreenIsDestination, SimpleExoPlayerView1, mp.SimpleExoPlayerView1)))
End Sub
 
Upvote 0

I don't know if that Sub works, but it assumed the player was in the B4XMainPage.
Since your player is in your B4xPage4tv page, you should write it the way I did. "mp" is and was superfluous even in the original Sub; it was just a convenience for writing "mp" instead of B4XPages.MainPage; you had a variable "mp" that pointed to the MainPage.
 
Upvote 0
Fixed:
B4X: Copy
Private Sub SwitchTargetPlayerView(FullScreenIsDestination As Boolean)
    Dim page4tv As B4XPage4tv = B4XPages.GetPage("TvPre") ' That is the specific page
    Dim jo As JavaObject
    Dim player As Object = page4tv.Player.As(JavaObject).GetField("player")
    jo.InitializeStatic("androidx.media3.ui.PlayerView")
    jo.RunMethod("switchTargetView", Array(player, IIf(FullScreenIsDestination, page4tv.SimpleExoPlayerView1, SimpleExoPlayerView1), IIf(FullScreenIsDestination, SimpleExoPlayerView1, page4tv.SimpleExoPlayerView1)))
End Sub

Where tvpre is: Public TvPre As B4XPage4tv
 
Upvote 0
Back
Top