Android Question Webview load local HTML

MichalK73

Well-Known Member
Licensed User
Longtime User
Hello.

How load local file HTML writing w DirAssets to WebView ?

Code: Copy
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private WebView1 As WebView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim url As String= File.DirAssets&"opis.html"
    WebView1.LoadHtml(url)
    Activity.LoadLayout("form_info")
End Sub
webview1 designer is stretched to fill the screen.
I displays error:
Code: Copy
Copying updated assets files (1)
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = false **
** Activity (info) Create, isFirst = true **
Error occurred on line: 23 (info)
java.lang.RuntimeException: Object should first be initialized (WebView).
    at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
    at anywheresoftware.b4a.objects.WebViewWrapper.LoadHtml(WebViewWrapper.java:117)
    at com.wmk.lok.info._activity_create(info.java:346)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:702)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at com.wmk.lok.info.afterFirstLayout(info.java:102)
    at com.wmk.lok.info.access$000(info.java:17)
    at com.wmk.lok.info$WaitForLayout.run(info.java:80)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5461)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:134)
** Activity (info) Resume **
 
Hi, just switch those two lines:
Code: Copy
WebView1.LoadHtml(url)           ' old position
Activity.LoadLayout("form_info")
WebView1.LoadHtml(url)           'new position

You added WebView1 by the Designer and it gets initialized when you load the layout containing it. So, you first load a layout then use views that are part of it. Nothing special.

Edit: just read it with greater attention now. You should use LoadURL not LoadHTML.
 
Upvote 0
ok.
Now another problem. After changing the code, change the url value is odd, there is no full path to the file.
Code: Copy
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private WebView1 As WebView

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.LoadLayout("form_info")
    Dim url As String= File.DirAssets&"opis.html"
    Log(url)
    WebView1.LoadUrl(url)
   
End Sub

LOG:

Code: Copy
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
** Activity (main) Pause, UserClosed = false **
** Activity (info) Create, isFirst = true **
AssetsDiropis.html
** Activity (info) Resume **

In webview1 display only 'AssetsDiropis.html'
 
Upvote 0
Hi, use syntax like
Code: Copy
webview1.LoadUrl("file:///android_asset/myfile.htm")
It should work also:
Code: Copy
webview1.LoadUrl("file://" & File.Combine(File.DirInternal, "myfile.html"))
 
Upvote 0
Ok, I will check.
I think also I found another solution. I used method LoadHtml.
And it works as it should.
Code: Copy
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Activity.LoadLayout("form_info")
    'Dim url As String= File.DirAssets&"\opis.html"
    Dim text As String=File.GetText(File.DirAssets,"opis.html")
    'Log(url)
    Log(text)
    'WebView1.LoadUrl(url)
    WebView1.LoadHtml(text)
   
End Sub

Thank you so much for help.
 
Upvote 0
OK udg beat me to it, as I was going to suggest:

Code: Copy
WebView1.LoadUrl("file://" & File.DirAssets & "/example.html")

Remember you cannot edit or save a file in the Assets directory!
 
  • Like
Reactions: udg
Upvote 0
@udg
WebView1.LoadUrl("file://" & File.Combine(File.DirAssets, "opis.html")) = error "You can not load a page file://assetdir/opis.html"

@mark35at
WebView1.LoadUrl("file://" & File.DirAssets & "/opis.html")= error "You can not load a page file://assetdir/opis.html"
 
Upvote 0
Upvote 0
Back
Top