WebView pathway

125 views
Skip to first unread message

Isaac Franks

unread,
Jul 11, 2019, 3:55:01 PM7/11/19
to DroidScript
I have an app that uses more than one page, but with the current bug I need to use WebViews. They seem simple enough, but I have never used them and when I used the code from the sample, it was not able to find the pathway. I am using a tablet so maybe it would use a different pathway, but would anyone able to tell me whether it is more than Sys/HTML/(name of page)?

Symbroson

unread,
Jul 11, 2019, 9:14:40 PM7/11/19
to DroidScript
You can load html files from anywhere. The "/Sys/*" path is for default assets shipped with DroidScript. You can't put files there.
But I guess you want to load html files from your project directory.

The suggested path for html files is "Html/page.html".
However if you don't want to change the folder structure of your project the main html file will still be located in your project root, meaning that you can just load "page.html" to your webview.

Take a look at your project folder with a file browser and you'll see what I mean

Isaac Franks

unread,
Jul 12, 2019, 11:37:48 AM7/12/19
to DroidScript
I want to load the pages that I have in the app in WebView, yes. The manner I do this, so long as it does not alter appearance or interface, is fine.  I currently have the following below. You'll notice a couple commented out lines where I was fiddling with code, but was unable to get it to work.

function OnStart()
{
    //Create a layout with objects vertically centered.
    lay = app.CreateLayout( "linear", "VCenter,FillXY" );   

    //Create a web control.
    web = app.CreateWebView( 0.8, 0.8 );
    web.SetOnProgress( web_OnProgess );
    lay.AddChild( web );
   
    //Create horizontal sub-layout for buttons.
    layHoriz = app.CreateLayout( "linear", "Horizontal" );   
   
    //Create 'Local' button.
    btnLocal = app.CreateButton( "Local" );
    btnLocal.SetOnTouch( btnLocal_OnTouch );
    layHoriz.AddChild( btnLocal );
   
   
    //Add horizontal layout to main layout.
    lay.AddChild( layHoriz );
   
    //Add layout to app.   
    app.AddLayout( lay );
}

//Called when user touches 'Local' button.
//(We could use a url like "file:///sdcard/MyPage.htm")
function btnLocal_OnTouch( item )
{
  app.ShowProgress( "Loading..." );
   //web.LoadUrl( item + ".html"  ); 
  //web.LoadUrl( "file:///Sys/Internal Storage/Droidscript/page.js/Tips Guide.htm" );
     web.LoadUrl( "Html/Tips Guide.html" );
}

//Show page load progress.
function web_OnProgess( progress )
{
    app.Debug( "progress = " + progress );
    if( progress==100 ) app.HideProgress();
}

I am building directly off the WebView sample, but I just need to get it to work and allow me to display my pages before I can make it my own.
Message has been deleted

John

unread,
Jul 12, 2019, 6:52:19 PM7/12/19
to DroidScript
Why use web.LoadUrl("file://" + path + file) when web.LoadUrl( file ) works just as well for files in the same directory as the app script?

Symbroson

unread,
Jul 12, 2019, 7:14:44 PM7/12/19
to DroidScript
Yes you're right. Forget what I said about the required absolute paths - they arent. Although they would work as well.
Here's the updated example anyway

var html = "<center>Hello World</center>";
var file = "File.html";

function OnStart()
{
   
// creates the file /sdcard/DroidScript/projectname/File.html
    app
.WriteFile( file, html )

    lay
= app.CreateLayout( "linear", "VCenter,FillXY" );

    web
= app.CreateWebView( 0.8, 0.8 );

    web
.SetOnProgress( web_OnProgess );
    lay
.AddChild( web );

    app
.AddLayout( lay );
   
    app
.ShowProgress();
    web
.LoadUrl( file );

Isaac Franks

unread,
Jul 13, 2019, 2:38:35 PM7/13/19
to DroidScript
And if I have multiple pages and want to view them, where would I refer to them in the code? Would I refer to them like web.LoadURL ("Page1.html, Page2.html")? I am just not certain where I would be able to modify individual pages

Steve Garman

unread,
Jul 13, 2019, 4:04:40 PM7/13/19
to DroidScript

If all you are trying to work out is how to load different local html files into the same webview at different times, the attached spk does that.

Perhaps it will clarify where the files are for you.

testPages.spk

Steve Garman

unread,
Jul 14, 2019, 2:08:21 AM7/14/19
to DroidScript
There is a problem with my spk that it allows the button to be pressed when the page is not fully loaded, causing a "_Run not found" error.

To prevent problems of that sort crashing the app, it is best to create the WebView with the ignoreErrors option

web = app.CreateWebView( -1,-1, "fillxy,ignoreErrors" );

Isaac Franks

unread,
Jul 16, 2019, 5:54:05 PM7/16/19
to DroidScript
Thanks for the edit Steve. Thanks John and Symbroson for the help!
Reply all
Reply to author
Forward
0 new messages