Back

CreateWebView

Hello World
Content:
- Methods

You can create a control to display local or remote web pages in your App using the CreateWebView method.

web = app.CreateWebView( width, height, options, zoom ) → app object: WebView

If you are loading remote web pages, then you might want to use the SetOnProgress method to set the name of a callback function that you want called to report the progress of loading the page.

You can use the LoadUrl method to load an internal or external web page or the LoadHtml method to load text directly from within your App.
web.LoadUrl( url, options );
web.LoadHtml( html, baseFolder, options );

If you need to, you can use the Execute method to execute JavaScript code within the WebView.
web.Execute( text );

Example - Remote

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

     web = app.CreateWebView( 1, 1, "Progress" );
     web.SetOnProgress( web_OnProgess );
     lay.AddChild( web );


     app.AddLayout( lay );

     web.LoadUrl( "http:///www.google.com" );
}

function web_OnProgess( progress )
{
     app.Debug( "progress = " + progress );
}
    Copy     Copy All       Run      

Example - Local File

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

     web = app.CreateWebView( 0.8, 0.8 );
     web.SetBackColor( "#00000000" );
     lay.AddChild( web );


     app.AddLayout( lay );

     web.LoadUrl( "file:///Sys/Html/Page.htm" );
}
    Copy     Copy All       Run      

Example - Direct Html

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

     web = app.CreateWebView( 0.8, 0.8 );
     web.SetBackColor( "#00000000" );
     lay.AddChild( web );


     app.AddLayout( lay );

    html = "<html><body><center>" +
        "    <img src='Img/Droid2.png'>" +
        "    <div style='color:white; font-size:30;'>" +
        "    Hello World!</div><br>" +
        "</center></body></html>";

     web.LoadHtml( html, "file:///Sys/" );
}
    Copy     Copy All       Run      

Methods

The following methods are available on the WebView object:

Back()
CanGoBack() → boolean
Focus()
GetAbsHeight() → number: integer
GetAbsWidth() → number: integer
GetHeight( options ) → number
GetLeft( options ) → number
GetParent() → app object
GetPosition( options ) → object: { left, top, right, bottom }
GetTop( options ) → number
GetType() → string: “WebView”
GetUrl() → string: url path
GetVisibility() → string: “Show” or “Hide” or “Gone”
GetWidth( options ) → number
Gone()
Hide()
IsEnabled() → boolean
IsOverlap( obj, depth ) → boolean
IsVisible() → boolean
Method( name, types, p1, p2, p3, p4 ) → all types
Print()
Reload()
SetScale( x, y )
Show()
boolean
app object
number
string
unknown
number: factor
number: fraction (0..1)
number: integer
number: milliseconds
number: percent
number: -180..180
number: -100..100
number: 0..100
number: 0..0.99 or 1..256
string:
  hexadecimal: “#rrggbb”, “#aarrggbb”
  colourName: “red”, “green”, ...
string: comma “,” separated
string: html code
string: javascript code
string: path to file or folder ( “/absolute/...” or “relative/...” )
string: url path
string: comma “,” separated: AllowZoom, “AutoZoom”, Wide, “FillX/Y”, NoActionBar, “IgnoreErrors”, “IgnoreSSLErrors”, NoApp, NoCapture, NoLocate, “NoLongTouch”, NoPause, “NoScrollBars”, Overview, Progress, “ScrollFade”, UseBasicInput, UseBrowser
string: “px”
string: “screen”, “px”
string: “left-right” or “right-left” or “top-bottom” or “bottom-top” or “bl-tr” or “br-tl” or “tl-br” or “tr-bl”
string: “repeat”
string: “Add” or “Multiply” or “clear” or “darken” or “lighten” or “overlay” or “screen” or “xor” or “src_in” or “src_out” or “src_atop” or “src_over” or “dst_in” or “dst_out” or “dst_atop” or “dst_over”
string: “px” or “sp” or “dip” or “mm” or “pt”
string: “px” or “sp” or “dip” or “dp” or “mm” or “pt”
string: “Show” or “Hide” or “Gone”
string: “META_META_ON”, “META_RIGHT_ON”, “NUM_LOCK_ON”, “SCROLL_LOCK_ON”, “SHIFT_LEFT_ON”, “SHIFT_MASK”, “SHIFT_ON”, “SHIFT_RIGHT_ON”, “SYM_ON”
string: “Linear.None” or “Quadratic.In/Out” or “Cubic.In/Out” or “Quartic.In/Out” or “Quintic.In/Out” or “Sinusoidal.In/Out” or “Exponential.In/Out” or “Circular.In/Out” or “Elastic.In/Out” or “Back.In/Out” or “Bounce.In/Out”
object: { source, action, count, x: [ x1, x2, x3 ], y: [ y1, y2, y3 ] }
object: { x, y, w, w, sw, sh, rot }
list: boolean,char,byte,short,int,long,float,double,String,CharSequence,...
function( result )
function( msg )
function( error )
function( progress )
function( event )
function()
web.AdjustColor
Adjust the visual color effect of the control by setting the Hue (by angle in degrees in a color circle), the saturation, brightness and contrast of the control.
web.Back
Loads the previous page from history.
web.CanGoBack
Check whether the user can travel back in history.
web.CanGoForward
Check whether the user can travel forward in history.
web.Capture
Captures the visible area of the web view to a file.
web.ClearFocus
Removes the focus of the control so that the user no longer has immediate access to it.
web.ClearHistory
Clears the page history for Back() and Forward()
web.Execute
Executes JavaScript code inside the WebView and passes the result to the callback
web.Focus
Set the focus to the control so that the user can interact with it immediately.
web.Forward
Loads the following page from history.
web.GetAbsHeight
Get the absolute height of the control in pixels.
web.GetAbsWidth
Get the absolute width of the control in pixels.
web.GetHeight
Get the height of the control as screen height relative float or in pixels with the px option.
web.GetLeft
Get the distance from the control to the left parent border as width relative float or in pixels with the px option.
web.GetParent
Returns the parent control object where the object was added to - commonly a layout.
web.GetPosition
Returns data about the position and size of the control.
If the screen option is given the position on the screen will be returned. Otherwise relative to the parent control.
The px options turns the relative values into pixels.
web.GetTop
Get the distance from the control to the upper parent border as height relative float or in pixels with the px option.
web.GetType
Returns the control class name.
web.GetUrl
Returns the current website url.
web.GetVisibility
Returns the current visibility state of the control. The Values are:
Show: visible
Hide: invisible but still consuming space
Gone: invisible and not consuming space
web.GetWidth
Get the width of the control as screen width relative float or in pixels with the px option.
web.Gone
Hides the control without consuming any more layout space as if it were never there.
web.Hide
Hide the control but keep the layout space free.
web.IsEnabled
Returns whether the control is currently useable by the user.
web.IsOverlap
Returns whether the control overlaps with another by a given distance.
web.IsVisible
Returns whether the control is currently visible to the user, ignoring overlaying controls.
web.LoadHtml
Loads html code to the WebView.
web.LoadUrl
Loads an url to the WebView
web.Method
Allows access to other functions defined on the object in Java via reflection.

Note: This function is a premium feature. Please consider subscribing to Premium to use this feature and support DroidScript in its further development.
web.Print
Print current page via the builtin android printer dialog.

Note: Available since Android 4.4 KitKat
web.Reload
Reloads the current page
web.SetBackAlpha
Set the transparency of the background by an alpha value between 0 (no transparency) and 0.99 (full transparent) or 1 (no transparency) and 256 (full transparent)
web.SetBackColor
Changes the background color of the control.
web.SetBackGradient
Define the background color of the control with a gradient. The default gradient direction is from top to bottom, but you can change it from left to right and the reversed versions of course.
web.SetBackGradientRadial
Define a radial color gradient for the background of control.
web.SetBackground
Changes the background to an image which can be repeated using the repeat option.
An image which is often used with that option is '/res/drawable/pattern_carbon' - try it out!
web.SetColorFilter
Adjust the visual color effect with a color and a given BlendMode. More information about BlendMode can be found in the Android Developer page.
web.SetEnabled
En/Disable the control physically and visually so that the user can/can not access the control. Events like OnTouch will still be fired.
web.SetMargins
Define a distance to other controls on each side of the control.
web.SetOnConsole
Define a callback function which is called when a message was logged to the console.
web.SetOnError
Define a callback function which is called when an error occured.
web.SetOnProgress
Define a callback function which is called when the webpage loading progress changed
web.SetOnTouch
Define a callback function that is called when the user touches the control. In addition, an event object is passed to the callback function to obtain information about the touch type, the touch position(s), the amount of touches and the control that was touched.
web.SetPadding
Define distances that elements within the control are to maintain from the control borders.
web.SetPosition
Defines the position and size for the control if the parent is an absolute layout.
web.SetRedirect
Add a url redirection rule.
web.SetScale
Scales the control along with its contents by the factors passed to the function.
web.SetSize
Change the size of the control in either screen relative values or in pixels if the px option was given.
web.SetTouchMode
Set the control in touch mode.
web.SetUserAgent
Defines a custom user agent
web.SetUserCreds
Set login data for websites that require authentication.
web.SetVisibility
Change the visibility of the control to one of the available modes:
Show: visible
Hide: invisible but still consuming space
Gone: invisible and not consuming space
web.Show
Set the visibility of the control to “Show”.
web.SimulateKey
Simulates a key event on an app object.

You can find the complete list of key names on the Android Developer Page.
web.Tween
Performs an animation on the control.
The target object is for the position, size and rotation that the control has at the end of the animation.

The type specifies the behavior and the speed of the animation. Separated by a dot, you must also specify whether you want to apply this behavior to the beginning (In), end (Out), or to both (InOut) times of the animation.

With the amount of repeats you can control how many times you want to play the animation.

If you have jojo activated, the animation will alternate between forward and backward playback, so that if the repetition value is odd, the control will be at the start position again at the end of the animation.

Finally the callback function will be called after the animation has finished. Well, it's about time!