Android Tutorial WebView Tutorial

codemanager

Member
Licensed User
Longtime User
Hello,

I wanted to load a local html file in webview so I made an html file and put it in my files folder. The webview is there in my layout file. Then i used this method as shown
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
    WebView1.LoadURL("file:///android_asset/index.html")
End Sub

But the above code gives me "Webpage not available". The Web Page at file:///android_asset/index.html could not be loaded as: The requested file was not found. index.html

Am i missing something? All i want to do is to load a local html file into a webview. Also this html file has not images only text.

Thanks for guidance!
 

NJDude

Expert
Licensed User
Longtime User
Try this:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")

    myHTML = File.ReadString(File.DirAssets, "index.html")
    
    WebView1.LoadHTML(myHTML)
End Sub

If you have pictures, you will have to modify your HTML and point the source to the assets directory.
 

IanMc

Well-Known Member
Licensed User
Longtime User
That is strange.

You could try with other html files, also check that the index.html file hasn't been deleted as if you don't add it to your project and do a 'clean' this can happen.

NJ's tip should make it clearer if the problem is that it can't find the file (which is what it is saying).

Can you make a simple project, zip it up and post it here then we can analyse it.

Does my example work on your machine?
 

codemanager

Member
Licensed User
Longtime User
thanks for your help NJDude! your code worked perfectly : ) Also thanks for pointing out newbie resources as i'm new to B4Android and loving it, cheers
 

codemanager

Member
Licensed User
Longtime User
thanks for the example project IanMac! I downloaded WebView2 Example and when i run it i get to the activity where there are buttons for various pages but clicking on either of the buttons shows me the "Webpage not available". The Web Page at file:///android_asset/index.html could not be loaded as: The requested file was not found. I also checked with Files tab and all the html files are in there. I'll try it a couple of times again tomorrow and will update you! thanks for all the help and yes i'm new to b4android and loving it, cheers!
 

IanMc

Well-Known Member
Licensed User
Longtime User
Ok cool, glad you are loving B4a, we all are

An interesting problem you are having there so when we inevitably figure it out it will be interesting to find out what the problem is.
 

rbghongade

Active Member
Licensed User
Longtime User
Dear IanMc,
Thank you for the great tutorial on Webview.
I could generate the ebook , but missing out as to what template should I select so that the page fits to a cell phone screen. Your example of "A Turkey For One" is the one I expect of my ebook. If you could please upload one such example project (complete with source) , I would appreciate that greatly!
My intention is to create an ebook using the word file already typed by me.
regards,
 

IanMc

Well-Known Member
Licensed User
Longtime User
Hi RB

Just the fact that you have got this far tells me that you are a good programmer.

Keep at it, here is the source for A turkey for one.

Keep learning and soon you'll be earning

ian
 

Attachments

  • A_Turkey_for_one.zip
    29 KB · Views: 632

IanMc

Well-Known Member
Licensed User
Longtime User
Hey RB, I hope you noticed that it was really the story that you liked in my 'A turkey for one' and that made you look beyond the clunky way I displayed it.

I hope this will instill in you a desire to present a story in a much better way than I did it.

Let me know if I can help.
 

rbghongade

Active Member
Licensed User
Longtime User
Dear IanMc,
Thank you for your super-quick response. Because of people like you I dare to write programs! BTW I work as a Professor in Electronics & Telecom Engineering and desire to share my technical understanding with any and everyone! I want to write about the intricate concepts in easy language and distribute the same freely. Due to B4A and this forum, with friends like you and Erel , I , have found a way to do so!
Thanks once again!
regards,
 

coslad

Well-Known Member
Licensed User
Longtime User
I didn't understand a things .
Your html page fills horizontal the device display , i tried it on a galaxy s and on a note 3 .
How do you make it possible ?
 

luisftv

Member
Licensed User
Longtime User



This code did work for me too... but I have a few questions:

1. Why the code WebView1.LoadURL("file:///android_asset/index.html") does not work but the one using myHTML does?

2. In my code screen, the myHTL is red. Why? It does work though... I just want to understand.

3. Also, my html file contains a picture which loads correctly from withing the assets, but, after zooming the text I have to scroll left and right to read it. If I remove the picture, then the zoomed text gets automatically re-position on the screen and there is no need to scroll. While having the picture, how can I stop it from scrolling left and right so that it wraps and re-positions on the screen automatically.

4. I was also able to load an "mht" file, instead of an html. However, it did not display all the formatting correctly or the embedded pictures. Does Android support "mht" files? How?

Thanks.
 
Last edited:

Izmirov.Yaminovich

Member
Licensed User
Longtime User
I'm trying to stream ip camera using webview, it is possible? Because at the webview the layout for the web has been finish loading but in the area that show the live feed, it show "couldn't load the plug in"...is there any plug in for the webview to stream the ip camera or it must use videoview?
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…