Home Tutorials Hints & Tips

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.setWebViewClient(new ourBrowser());

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{
   public boolean shouldOverrideUrlLoading(WebView view, String 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

Privacy and Cookies Disclaimer Copyright
© 2015 - 2018 North Border Tech Training All rights reserved