Working with Android WebViews

Get introduced to the basic usage of a web view in Android and learn how to use the different customizations it offers.

Introduction

In this lesson, we’ll learn how to display web content within our app. The Android SDK comes bundled with the WebView class, which allows us to display web pages in the activity layout. By default, the WebView class simply shows a web page and doesn’t include features such as navigation controls, address bars, and so on. We can customize the experience by building on top of the base class. We’ll first see an example of how to embed a web page inside our app, and then we’ll see how to parse HTML content and display it in a WebView. We’ll also go over some of the common customizations offered by the WebView class.

Working with WebView

In this section, we’ll go through the basic usage of the WebView element.

Displaying a web page

We need to add the WebView element in the activity layout to display web pages.

<WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Next, in the activity class file, we’ll use the loadUrl method to specify the web URL to be displayed in the WebView.

val myWebView: WebView = findViewById(R.id.webview)
myWebView.loadUrl("https://www.educative.io/")

Displaying custom HTML content

To display HTML content, we need to use the loadData method.

Get hands-on with 1400+ tech skills courses.