I am using Codename One and I want to insert a lot of html pages in my app and then load them in a WebView. I want to insert html files in resources. How can I insert files in resources, maybe by using GUI builder data? What is the code to load the html files in WebView?
1 Answers
There are several ways to do this. If you can package every individual file into a single file you can use something like this:
- put the
test.html
in default package - create
WebBrowser webBrowser = new WebBrowser();
and add to your form webBrowser.setUrl("jar:///test.html")
If you need a complex HTML/CSS/JavaScript hierarchy then you can place all your files into the root html package (src/html
).
Then open an HTML file from within the html package using code like this:
try {
browserComponent.setURLHierarchy("/htmlFile.html");
} catch(IOException err) {
...
}
Notice that the path is relative to the html directory and starts with /
but inside the HTML files you should use
relative (not absolute) paths.
The build server will
tar
the entire content of that package and add anhtml.tar
file into the native package. Thistar
is seamlessly extracted on the device when you actually need the resources and only with new application versions (not on every launch).
From the manual.
As a sidenote the resource files do support adding arbitrary data files but the hierarchies solution is a better approach.

- 51,749
- 5
- 35
- 65

- 1,907
- 11
- 16
-
I added two other options for the answer – Shai Almog Feb 18 '16 at 03:56
-
I use a WebBrowser component with this code in my StateMachine.java file: @Override protected boolean onMainItem1() { String baseUrl = "jar:///Prova.html"; findWebBrowser().setURL(baseUrl); return true; } this code works in simulator but doesn't in build. I want to insert a site in my app (a complex HTML/CSS/JavaScript hierarchy). – giovanni_ Feb 21 '16 at 00:38