Android Question Changing the dimension of the Webview through screen orientation

Mattiaf

Active Member
Licensed User
Hi, I'm trying to change the dimension of the webview through screen orientation without making the stream in webview to stop.
Erel wrote this code in 2016:

Add this line to the manifest editor:

B4X:
SetActivityAttribute(Main, android:configChanges, "orientation|screenSize")

B4X:
Sub Globals
   Private ime1 As IME
   Private WebView1 As WebView
   Private ActivityParent As JavaObject
End Sub

Sub Activity_Create(FirstTime As Boolean)
   WebView1.Initialize("WebView1")
   Activity.AddView(WebView1, 0, 0, 100%x, 100%y)
   ime1.Initialize("ime1")
   ime1.AddHeightChangedEvent
   WebView1.LoadUrl("http://www.google.com")
   Dim jo As JavaObject = Activity
   jo.RunMethodJO("getContext", Null).RunMethodJO("getWindow", Null).RunMethod("setSoftInputMode", _
     Array As Object(0x20))
   ActivityParent = jo.RunMethodJO("getParent", Null)
End Sub
Sub IME1_HeightChanged (NewHeight As Int, OldHeight As Int)
   CallSubDelayed(Me, "AfterChange")
End Sub

Sub AfterChange
   Dim ajo As Panel = Activity
   Dim width As Int = ActivityParent.RunMethod("getMeasuredWidth", Null)
   Dim height As Int = ActivityParent.RunMethod("getMeasuredHeight", Null)
   If width = 0 OR height = 0 Then Return
   ajo.Width = width 'update the "activity" width and height
   ajo.Height = height
   WebView1.Width = width
   WebView1.Height = height
End Sub

Sub Activity_Resume
   AfterChange
End Sub

but he also said:

I don't recommend using the solution in this thread unless it is critical that the WebView will not be reloaded. It breaks that standard program flow as the Activity is not recreated as it should be.
because
you will need to manually update the layout. Another possible solution is to lock this specific activity orientation.

So I tried as he stated to
update the layout in AfterChange sub. Call Activity.RemoveAllViews before you load the new layout.

and to be honest, this latter didn't help me out.

I wonder if 6 year after there is a solution..
This is a video of how my app is working


As you can see the layout is being created with the WebView at the top, then I rotate the screen in landscape fullscreen ( even though it is not possible to see through the video..) and the again at portrait, but the webview doesn't come back to the original position at the top and as you can see, it stays inside the "fullscreen mode"...


Another code I'm trying is this one ( which works for changing rotation, but I still can't find the right sizes)

B4X:
Private screenwidth As Int = GetDeviceLayoutValues.Width
    Private screenheight As Int = GetDeviceLayoutValues.Height
    
    Private left As Int = (screenwidth/2)-(WebView1.Width/2)
    Private top As Int = (screenheight/2)-(WebView1.height/2) -40
    If (screenwidth < screenheight) Then    ' portrait
        WebView1.SetLayout(0dip,0dip,WebView1.width,WebView1.height)
    Else
        WebView1.SetLayout(left, top, screenwidth,screenheight)
    End If

it works on portrait, but on fullscreen landscape

Is there a way to fix it? Thanks
 
Last edited:

moster67

Expert
Licensed User
Longtime User
I wrote a solution ages ago for VideoView:

It's based on some of the code you showed in your post but also uses a frame layout with the help of the XmlLayoutBuilder.
 
Upvote 0

moster67

Expert
Licensed User
Longtime User
with that code is the Webview going to witdraw itself for every orientation changing?
I have no idea since I have no experience with Webview. Since you seemed stuck, I just thought I would share a solution that resolved a similar problem.
You will need to test it and then you can let us know if it works.
 
Upvote 0
Top