Android 4.4 (API level 19) introduces a new version ofWebView
that is based on Chromium. This change upgradesWebView
performance and standards support for HTML5, CSS3, and JavaScript to match the latest web browsers. Any apps using WebView
will inherit these upgrades when running on Android 4.4 and higher.
> If you call methods on WebView
from any thread other than your app's UI thread, it can cause unexpected results. For example, if your app uses multiple threads, you can use the runOnUiThread()
method to ensure your code executes on the UI thread:
runOnUiThread(new Runnable() { @Override public void run() { // Code for WebView goes here } });
Also be sure that you never block the UI thread. A situation in which some apps make this mistake is while waiting for a JavaScript callback. For example, do not use code like this:
// This code is BAD and will block the UI thread webView.loadUrl("javascript:fn()"); while(result == null) { Thread.sleep(100); }
> The result of the user clicking such a link can vary:
- If you loaded the page by calling
loadData()
orloadDataWithBaseURL()
with an invalid or null base URL, then you will not receive theshouldOverrideUrlLoading()
callback for this type of link on the page.Note: When you use
loadDataWithBaseURL()
and the base URL is invalid or set null, all links in the content you are loading must be absolute. - If you loaded the page by calling
loadUrl()
or provided a valid base URL withloadDataWithBaseURL()
, then you will receive theshouldOverrideUrlLoading()
callback for this type of link on the page, but the URL you receive will be absolute, relative to the current page. For example, the URL you receive will be"http://www.example.com/showProfile"
instead of just"showProfile"
.
> You can then handle this URL in your shouldOverrideUrlLoading()
method like this:
// The URL scheme should be non-hierarchical (no trailing slashes) private static final String APP_SCHEME = "example-app:"; @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.startsWith(APP_SCHEME)) { urlData = URLDecoder.decode(url.substring(APP_SCHEME.length()), "UTF-8"); respondToData(urlData); return true; } return false; }
If you can't alter the HTML then you may be able to use loadDataWithBaseURL()
and set a base URL consisting of a custom scheme and a valid host, such as "example-app://<valid_host_name>/"
. For example:
webView.loadDataWithBaseURL("example-app://example.co.uk/", HTML_DATA, null, "UTF-8", null);
> If you cannot set the width of the viewport in the HTML, then you should call setUseWideViewPort()
to ensure the page is given a larger viewport. For example:
WebSettings settings = webView.getSettings(); settings.setUseWideViewPort(true); settings.setLoadWithOverviewMode(true);
> You can handle this change in the following ways:
- Alter the styles of your application:
If you have control of the HTML and CSS on the page, you may find that altering the design of your content may be the most reliable approach. For example, for screens where you cite licenses, you may want wrap text inside of a
<pre>
tag, which you could do with the following styles:<pre style="word-wrap: break-word; white-space: pre-wrap;">
This may be especially helpful if you have not defined the viewport properties for your page.
- Use the new
TEXT_AUTOSIZING
layout algorithm:If you were using narrow columns as a way to make a broad spectrum of desktop sites more readable on mobile devices and you aren't able to change the HTML content, the new
TEXT_AUTOSIZING
algorithm may be a suitable alternative toNARROW_COLUMNS
.