public class

WebView

extends View
java.lang.Object
   ↳ com.neomades.ui.View
     ↳ com.neomades.ui.WebView

Class Overview

A View that displays web pages.

This class is the basis upon which you can roll your own web browser or simply display some online content within your Screen. It includes methods to navigate forward and backward through a history, zoom in and out, perform text searches and more. A WebView could load an URL with the loadURL(String) method or a part of HTML code with the loadHTML(String, String, String) method.
A WebView will enforce a MATCH_PARENT stretch mode in both directions.

WebView and network security

Permission

To use this class, the WEB_VIEW permission must be declared in the URS file.

App Transport Security for iOS

WebView connections, like other HTTP connections in the application, must follow the ATS requirements:

  • The HTTP connection must use HTTPS
  • The negotiated Transport Layer Security (TLS) version of the contacted server must be TLS 1.2.
  • The digital certificate of the server must follow the X. 509 norm and must be trusted by a certificate authority.
  • The leaf server certificate must be signed with RSA or ECC keys.
  • The connection must use either the AES-128 or AES-256 symmetric cipher.

If the back end server the WebView must contact is outside of your scope (a third party server for example), the ATS requirements can be bypassed by configuring the URS of the project. However, in that case, you will have to justify it in iTunes Connect when the application will be prepared for publication and Apple may reject the application.

Full documentation about ATS and network security are available at NeoMAD documentation.

Javascript and HTML errors

By default, a WebView provides no browser-like widgets, does not enable JavaScript and web page errors are ignored.

Summary

XML Attributes
Attribute Name Related Method Description
horizontalScroll setHorizontalScroll(boolean) If the scrollview could scroll horizontally  
openUrlEnabled setOpenURLEnabled(boolean)  
padding setPadding(int) eg: 10dp or 10 (dp is the default unit)  
paddingBottom setPaddingBottom(int) eg: 10dp or 10 (dp is the default unit)  
paddingLeft setPaddingLeft(int) eg: 10dp or 10 (dp is the default unit)  
paddingRight setPaddingRight(int) eg: 10dp or 10 (dp is the default unit)  
paddingTop setPaddingTop(int) eg: 10dp or 10 (dp is the default unit)  
readOnly setReadOnly(boolean) If the text inside is not editable  
textZoom setTextZoom(int)  
verticalScroll setVerticalScroll(boolean) If the scrollview could scroll vertically  
zoomEnabled setZoomEnabled(boolean)  
[Expand]
Inherited XML Attributes
From class com.neomades.ui.View
[Expand]
Inherited Constants
From interface com.neomades.ui.Alignment
From interface com.neomades.ui.StretchMode
Public Constructors
WebView()
Public Methods
boolean canGoBack()
Returns if this WebView can go back in the history.
boolean canGoForward()
Returns if this WebView can go forward in the history.
void clearCache()
Clears the cache of the WebView.
void clearCookies()
Clears all the stored cookie.
List<HttpCookie> getCookies()
Gets the cookies of the WebView for the current URL.
void goBack()
Goes back in the history of this WebView.
void goForward()
Goes forward in the history of this WebView.
void loadHTML(String html, String mimeType, String baseURL)
Loads the given data into this WebView using a 'base' scheme URL.
void loadURL(String url)
Loads the given URL.
void reload()
Reloads the current URL.
void setCookiePolicy(CookiePolicy policy)
Sets the cookie policy of the Webview.
void setHorizontalScroll(boolean enabled)
Sets if this WebView should display the horizontal scrollbar when scrolling.
void setOpenURLEnabled(boolean openUrlEnabled)
Sets if a click on a link should open the page in this WebView or in the browser.
void setPadding(int leftDp, int topDp, int rightDp, int bottomDp)
Has no effect on WebView.
void setPadding(int uniformPaddingDp)
Has no effect on WebView.
void setPaddingBottom(int paddingBottomDp)
Has no effect on WebView.
void setPaddingLeft(int paddingLeftDp)
Has no effect on WebView.
void setPaddingRight(int paddingRightDp)
Has no effect on WebView.
void setPaddingTop(int paddingTopDp)
Has no effect on WebView.
void setReadOnly(boolean readOnly)
Sets this WebView in read-only mode.
void setTextZoom(int textZoom)
By default, this property is 100 %.
void setVerticalScroll(boolean enabled)
Sets if this WebView should display the vertical scrollbar when scrolling.
void setZoomEnabled(boolean enabled)
Sets this WebView can zoom-in or zoom-out content.
void stopLoading()
Stops the current load.
Protected Methods
void onProgressChanged(WebView view, int progress)
Override this method in order to listen the loading progress.
boolean shouldOverrideUrlLoading(WebView webView, String url)
Give the host application a chance to take over the control when a new URL is about to be loaded in the current WebView.
[Expand]
Inherited Methods
From class com.neomades.ui.View
From class java.lang.Object

XML Attributes

horizontalScroll

If the scrollview could scroll horizontally

openUrlEnabled

Related Methods

padding

eg: 10dp or 10 (dp is the default unit)

Related Methods

paddingBottom

eg: 10dp or 10 (dp is the default unit)

Related Methods

paddingLeft

eg: 10dp or 10 (dp is the default unit)

Related Methods

paddingRight

eg: 10dp or 10 (dp is the default unit)

Related Methods

paddingTop

eg: 10dp or 10 (dp is the default unit)

Related Methods

readOnly

If the text inside is not editable

Related Methods

textZoom

Related Methods

verticalScroll

If the scrollview could scroll vertically

Related Methods

zoomEnabled

Related Methods

Public Constructors

public WebView ()

Public Methods

public boolean canGoBack ()

Returns if this WebView can go back in the history.

Returns
  • can go back

public boolean canGoForward ()

Returns if this WebView can go forward in the history.

Returns
  • can go forward

public void clearCache ()

Clears the cache of the WebView. Does not necessarily clears the cookies. Use clearCookies() to clear the cookies.

public void clearCookies ()

Clears all the stored cookie.

Cross platform considerations

On iOS this method does not work on simulator.

public List<HttpCookie> getCookies ()

Gets the cookies of the WebView for the current URL.

Cross platform considerations

On Android only the name and the value of the headers are returned.

Returns
  • the cookies of the WebView for the current URL

public void goBack ()

Goes back in the history of this WebView.

See Also

public void goForward ()

Goes forward in the history of this WebView.

See Also

public void loadHTML (String html, String mimeType, String baseURL)

Loads the given data into this WebView using a 'base' scheme URL.

The 'base' scheme URL formed by this method uses the default US-ASCII charset. If you need need to set a different charset, you should form a 'base' scheme URL which explicitly specifies a charset parameter in the mediatype portion of the URL and call loadUrl(String) instead. Note that the charset obtained from the mediatype portion of a data URL always overrides that specified in the HTML or XML document itself.

Cross Platform Considerations

The mimeType parameter is not used on the iOS platform.

Parameters
html HTML code
mimeType "text/html" for example. If null, default to "text/html".
baseURL base URL to use to resolve URL links into HTML code. Can be null
Throws
NullPointerException if the html parameter is null

public void loadURL (String url)

Loads the given URL.

Parameters
url the URL of the resource to load
Throws
NullPointerException if the url parameter is null

public void reload ()

Reloads the current URL.

public void setCookiePolicy (CookiePolicy policy)

Sets the cookie policy of the Webview. By default it is ACCEPT_ORIGINAL_SERVER.

Parameters
policy the cookie policy to set

public void setHorizontalScroll (boolean enabled)

Sets if this WebView should display the horizontal scrollbar when scrolling.

By default, this property is true.

Related XML Attributes

public void setOpenURLEnabled (boolean openUrlEnabled)

Sets if a click on a link should open the page in this WebView or in the browser.

By default, this property is false.

This property is used if shouldOverrideUrlLoading(WebView, String) returns false.

Related XML Attributes
Parameters
openUrlEnabled if true the link will be opened in the browser, else it will be opened in this WebView

public void setPadding (int leftDp, int topDp, int rightDp, int bottomDp)

Has no effect on WebView.

Parameters
leftDp the left padding (in dp)
topDp the top padding (in dp)
rightDp the right padding (in dp)
bottomDp the bottom padding (in dp)
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setPadding (int uniformPaddingDp)

Has no effect on WebView.

Related XML Attributes
Parameters
uniformPaddingDp the padding to set on each side (in dp).
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setPaddingBottom (int paddingBottomDp)

Has no effect on WebView.

Related XML Attributes
Parameters
paddingBottomDp value of the bottom padding (in dp)
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setPaddingLeft (int paddingLeftDp)

Has no effect on WebView.

Related XML Attributes
Parameters
paddingLeftDp value of the left padding (in dp)
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setPaddingRight (int paddingRightDp)

Has no effect on WebView.

Related XML Attributes
Parameters
paddingRightDp value of the right padding (in dp)
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setPaddingTop (int paddingTopDp)

Has no effect on WebView.

Related XML Attributes
Parameters
paddingTopDp value of the top padding (in dp)
Throws
CrossThreadException if the method is called from outside the UI-Thread

public void setReadOnly (boolean readOnly)

Sets this WebView in read-only mode. The use cannot click over links or buttons.

By default, this property is false.

Related XML Attributes

public void setTextZoom (int textZoom)

By default, this property is 100 %.

Limitations

In iOS, setting a zoom has no effect.

Related XML Attributes

public void setVerticalScroll (boolean enabled)

Sets if this WebView should display the vertical scrollbar when scrolling.

By default, this property is true.

Related XML Attributes

public void setZoomEnabled (boolean enabled)

Sets this WebView can zoom-in or zoom-out content.

By default, this property is true.

Related XML Attributes

public void stopLoading ()

Stops the current load.

Protected Methods

protected void onProgressChanged (WebView view, int progress)

Override this method in order to listen the loading progress.

When the loading starts, the first progress value to be sent to this callback may not be 0, this is platform implementation dependent.

When the loading finishes the last progress value is always 100.

Parameters
view this Webview
progress between a progress percent value always between 0 and 100

protected boolean shouldOverrideUrlLoading (WebView webView, String url)

Give the host application a chance to take over the control when a new URL is about to be loaded in the current WebView.

Parameters
webView the WebView that loads the new URL
url the URL to be loaded
Returns
  • true if the method has handled the URL and the WebView should not do the default behavior. Otherwise, the web view will load URL links inside