Webview not working

Brad

Active Member
Licensed User
Longtime User
The webview is a no-brainer but for some reason I can't get it to display any web pages. I was messing around with it and trying to load google but nothing loads.

I have a button that loads the url then displays the url in a label. For some reason the first time I click the button, the url is NULL.. Not till I click it the 2nd time will the url display correctly. :BangHead:
 

Brad

Active Member
Licensed User
Longtime User
Pretty basic code.

B4X:
Sub Globals
   Dim Button1 As Button
   Dim WebView1 As WebView
   Dim Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("main")
   WebView1.Initialize("WebView")

End Sub

Sub Button1_Click
   WebView1.LoadUrl("http://www.google.com")
   label1.Text = "URL: " & WebView1.Url
End Sub
 
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
If you have placed the WebView in the Designer, then you do not need
WebView1.Initialize("WebView")

If not, then change it to:
WebView1.Initialize("WebView1")
 
Upvote 0

Brad

Active Member
Licensed User
Longtime User
:sign0161: Thanks..still new to B4A :sign0104:

The url property still shows NULL after the first click..Not until the 2nd click will the page load and the url field displays correctly.
 
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
Confirmed. It appears as if WebView.Url is not available until a page is fully loaded. I can't find any event that is raised as a page is loaded, such as

Sub WebView1_Ready
Label1.text = WebView1.Url
End Sub
 
Upvote 0

alfcen

Well-Known Member
Licensed User
Longtime User
This is certainly not a critical issue.
You could use a timer, however, one doesn't know in advance how long it takes a page to load:

B4X:
Sub Globals
   Dim Button1 As Button
   Dim WebView1 As WebView
   Dim Label1 As Label
   Dim Timer1 As Timer
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("layout_main")
   Timer1.Initialize("Timer1",2000)
End Sub

Sub Button1_Click
   WebView1.LoadUrl("http://www.google.com")
   Timer1.Enabled = True
End Sub

Sub Timer1_Tick
   Label1.Text = "URL: " & WebView1.Url
   Timer1.Enabled = False
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can check if the URL is null and keep waiting:
B4X:
Sub Timer1_Tick
    If WebView1.Url = Null Then Return

Also note that timers should be declared as process global variables and be initialized when FirstTime is true (in Activity_Create).
Otherwise a new timer will be created each time the activity is recreated.
B4X:
Sub Process_Globals
    Dim Timer1 As Timer
End Sub

Sub Globals
    Dim WebView1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        Timer1.Initialize("Timer1", 500)
    End If
  ...
 
Upvote 0

Brad

Active Member
Licensed User
Longtime User
A timer would be good at setting a timeout so if the page won't load in say 30 seconds, the timer event could display a message saying that the server wasn't responding instead of having the user wait. Now, what happens if the url isn't valid?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…