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:
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
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
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
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
...
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?
The user will see the default "page not available" page. Unfortunately it is very difficult to get such errors with the WebView API (which behaves differently than documented in this case).