comet
Welcome to LanDen Labs
Source Code, Tools, Developer Notes and Performance Metrics

landenlabs.com

[ To Main Page ]


webviewtester.png

WebViewTester - WebView view object tester / demo

Dennis Lang https://landenlabs.com/index.html

 


WebViewTester - Features

WebViewTester is an app to help test how WebView view object behaves when renderning websites with different settings enabled. The app uses two fragments which control a WebView settings panel and a WebView object.

The settings fragment manages all of the WebView settings.
settings.png

The webview fragment is a combination of several controls.

  1. Combobox (AutocompleteTextView) to select URL
  2. Web navigation (previous, load, next)
  3. WebView to show web site/page
  4. Scrolling status/progress information
webview

Since the two sections are managed using fragments it is easy to make multiple presentations using different layouts. For exmample, on a wide device (tablet) in Landscape mode the two panels are displayed side-by-side landscape

[Top]


Notes on using WebView object

Using a WebView in your app to display internal assets such as a help page is straight forward. If you need to display Internet pages with links you may need additional logic to control where the page displays (in your webview or external browsers) By default web links will be displayed in an external browser unless you set the WebViewClient. The WebViewClient handles various events generated by the WebView. You can use it to control how the WebView handles link clicks and page redirects. Setting the default implementation of WebViewClient makes any URL open in the WebView rather then in an external browser:

// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
You can use the WebViewClient to selectively open links either in the webView or via external browser. To achieve this you need to extend the WebViewClient class and implement the shouldOverrideUrlLoading method. The shouldOverrideUrlLoading method is called whenever the WebView tries to navigate to a different URL. If it returns false, the WebView opens the URL itself. The default implementation always returns false, which is why it works in the previous example. public class MyAppWebViewClient extends WebViewClient {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if(Uri.parse(url).getHost().endsWith("myHost.com")) {
        return false;    // Special host open in webView
    }
    if(Uri.parse(url).getHost().length() == 0) {
        return false;    // Internal page open in webView
    }

    // Open in external browser
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    view.getContext().startActivity(intent);
    return true;
}

It is recommended that you enable JavaScript and DOM storage and set the Chrome agent to support the widest variety of websites.

[Top]


Reference / Links

  • Android WebView documentation
  • Pixel-Perfect UI in the WebView
  • WebView for Android