North Border Software




Home Tutorials Hints & Tips About

Android Apps and Training

Share on Facebook Share on Twitter

Getting Started in Android Development

Part 22 - The WebView Java

In the previous tutorial we put together a framework for the WebView. The final task is to place additional java code in the onClick method to enable the functionality.

We first need to get the URL from the edit text on the button click using the code below:

String url = urlText.getText().toString();

Next, there are a number of methods called to set the desired operating parameters of the WebView. There are other methods that can be used and so it is worth taking some time to explore other options here.

browser.getSettings().setLoadsImagesAutomatically(true);
browser.getSettings().setJavaScriptEnabled(true);
browser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
browser.setWebViewClient(new ourBrowser());
browser.loadUrl(url);

The first three and the last line (browser.loadUrl(url)) are fairly self explanatory. The line below is very important.

browser.setWebViewClient(new ourBrowser());

If we do not set a WebView client, then when the go button is clicked the url will be loaded into the default browser on the phone or tablet. Using the above method ensures that the WebView itself is used. We do need to create  private class. In this case we called it ourBrowser.  The code for this class is shown below:

private class ourBrowser extends WebViewClient{
   @Override
   public boolean shouldOverrideUrlLoading(WebView view, String url) {
      view.loadUrl(url);
      return true;
   }
}

The override method shouldOverrideUrlLoading ensures that the WebView loads the url. A screenshot of the resultant tutorial is shown below:

The working android webview layout with access to the Internet

After this tutorial your WebViewActivity.java class should look similar to the one below:


Download Download tutorial set 1

© 2015 North Border Software All rights reserved
Privacy and Cookies Terms of Use Disclaimer Copyright
North Border Software Logo