Android: Basic WebView Tutorial

A WebView lets you pull a web page and display it in your Android app. It doesn’t take long to implement an Android WebView. Read on. I’ll show you how.

AndroidManifest.xml

First, open your manifest file to add this permission for INTERNET:

The web page won’t be loaded into your app without this permission.

This is how my manifest file looks like:

Path: ...\MyAndroidStudioProjects\WebView\app\src\main\AndroidManifest.xml

activity_main.xml

Then we’re adding a WebView element to the XML file. My XML file is named activity_main.xml

Path: ...\MyAndroidStudioProjects\WebView\app\src\main\res\layout\activity_main.xml

MainActivity.java

I have MainActivity.java here, which loads activity_main.xml and then loads a web page (google.com) into the WebView.

Path: ...\MyAndroidStudioProjects\WebView\app\src\main\java\net\catzie\webview\MainActivity.java

The following line of code attaches the WebView whose id is “webview” to the variable “webView”:

And this allows JavaScript in the WebView:

This one is what actually loads a given URl into the WebView:

Related Posts:

Posts that may be related to "Android: Basic WebView Tutorial":

Catzie

A Filipino programmer with a variety of interests such as baking, singing, making up silly song/rap lyrics, K-pop, drawing, creating unique dessert flavors, obsessing about finding out how some things works, board games, anime, video games, and forgetting things that usually go in her long list of interests. Running small-time online dessert shops Cookies PH and Catzie's Cakery.

Leave a Reply

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