WebView in Android Example

An Android application is a world of its own. What is more interesting is that some of these “worlds” allow you to travel to another world, i.e. a website. The Android apps that let us do this use an extraordinary tool called WebView.

Needless to say, this feature is incredibly important as it lets us perform such a huge task. For this reason, Android app developers really need to study Web View properly in order to create multifunctional apps that provide a great user experience. In this article, we are going to learn the basics of the WebView tool and how you can apply it in your Android application.

WebView in Android

In Android development, the Web View tool allows an app developer to integrate a web page as a part of his application. The integrated web page can either be loaded from the Android application or the URL of the web page. In simpler words, Web View is used to display online content in an Android app activity, or you can say that it turns an Android app into a web app.

Not only that, a Web View also provides many additional features that a desktop browser has, such as cookies, history management, HTML5 support and many more. Also, by using Web View, we can specify an HTML string in order to show it in one of our application activities.

To display a web page in an app, the Web View tool in Android has to use the Webkit engine. In the android.webkit package, Web View is a subclass of the Absolute Layout class in Android. The Absolute Layout class is a subclass of ViewGroup, which is a subclass of View. With this chain of classes, the Android Web View component becomes a complete browser embedded into an application.

Web View vs Text View

Both Web View and Text View widgets have been here since Android API 1 and used for similar purposes. Naturally, Text View is used for text rendering, whereas Web View is used for web page rendering. However, if used smartly, both can be used for rich text and image rendering.

Simple formatting, such as font faces, styles, colors, links, etc., can rendered by Text View. It also handles only a limited set of HTML tags. With Text View, separate widgets have to be used for fetching images and intercepting hyperlinks. Also, Text View consumes significantly more memory than Web View. For these reasons, browsing sites like Facebook is not possible with a Text View. Instead, in such cases, Web View is the obvious choice.

In contrast to Text View, Web View can handle more complex text formatting. Not only that, the Web View widget supports most HTML tags as it is meant for HTML display. In addition to that, Web View can also be used to handle JavaScript and CSS, which is not possible with Text View. Furthermore, Web View can also help with the usual browsing metaphors, like the history of visited URLs. This is particularly useful for forward and backward navigation. Even though, Web View has been optimized to be memory efficient, it uses CPU (Central Processing Unit) and GPU (Graphics Processing Unit) more than Text View.

Web View in Android Steps

If you wish to implement Web View in your Android application, you will need to use the Android Studio IDE (Integrated Development Environment). Therefore, you may start by opening the Android Studio IDE.

Then, you need to “create a new project” on the Android Studio IDE. For this step, our earliest tutorials would be helpful for you. As you are creating a project, you will be asked to enter a project name and package name for your project.

We have set “webview” as our project name and “com.androidaura.webview” as our package name.

After creating a project, open your MainActivity.java class file so you can enter the following java code:

With this code, the basic template of our project has been developed. As you can see in the code, we have used the loadUrl() method of the Android Web View class in order to load the web page. The web page we have loaded is “https://www.androidaura.com.” Apart from simply loading the URL, we can have more control over our Web View by using different methods.

Now, you must open the activity_main.xml file in order to add the following code:

In the next step, you have to open your AndroidManifest.xml file and add the code mentioned below:

The Android Manifest code is important. If you wish for one of your Activities to access the internet and load web pages, you need internet permissions. These permissions are added in the Android Manifest file.

The “” part in our Manifest file defines internet permission to access it in our project.

Once you have entered all your codes in the right files, your project will be ready to run. So launch your project in this step and you will get an output that looks like this:

Output

webview in android