Build web apps in WebView

If you want to serve a web application (or just a web page) as part of a client application, you can do so using the WebView. The WebView class is an extension to the Android View class that allows you to display web pages as part of your activity’s layout. It does not include any features of a full-fledged web browser, such as navigation controls or an address bar. All the WebView does, by default, is display a web page.

A common scenario where using the WebView is useful is when you want to provide information in your application that you might need to update, such as a end user agreement or a user guide. Within your Android application, you can create an activity that contains a WebView and then use it to display your document that is hosted online.

Another scenario where the WebView can help is if your application provides data to the user who always requires an Internet connection to retrieve data, such as email. In this case, you might find that it’s easier to create a WebView in your Android app that displays a web page with all the user data, rather than making a network request, then parsing the data and rendering it in a layout. from android. Instead, you can design a web page to suit Android devices, and then implement a WebView in your Android app that loads the web page.

See Also:  How to sew a button on a pair of pants

This document shows you how to get started with a WebView and how to do a few extra things, like handling page navigation and linking JavaScript from your web page to client-side code in your Android app.

Adding a WebView to your app

To add a WebView to your application, you can include theFor example, your JavaScript code can call a method in your Android code to display a dialog, instead of using JavaScript’s alert() function.

To bind a new interface between your JavaScript and Android code, call addJavascriptInterface( ), passing it a class instance to bind to your JavaScript and an interface name that your JavaScript can call to access the class.

For example, you can include the following class in your Android application:

Caution: If you have set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method you want to make available to your JavaScript , and the method must be public. If you do not provide the annotation, your web page will not be able to access the method when running on Android 4.2 or higher.

In this example, the WebAppInterface class allows the web page to create a Toast message, using the showToast() method.

You can link this class to the JavaScript running in your WebView with addJavascriptInterface() and name the interface Android. For example:

This creates an interface called Android for JavaScript that runs on the WebView. At this point, your web application has access to the WebAppInterface class. For example, here’s some HTML and JavaScript that creates a toast message using the new interface when the user clicks a button:

See Also:  Creating a website 101

For example, the WebView may not call its shouldOverrideUrlLoading() method for links like this:

Leave a Reply

Your email address will not be published. Required fields are marked *