Article Fragment Layout
Learn how to implement a WebView in our application.
We'll cover the following...
We'll cover the following...
WebView in Android
WebView is an extension of Android’s View class that is used to render web pages directly in our application without the need to launch the browser, although they don’t offer all of the functions of a browser.
We’ll learn how WebView function by creating a basic standalone project that will only take a few minutes to set up and compile.
Simply create a new project and name it WebView, then let gradle build it. After a successful build, navigate to the activity_main.xml file and insert a WebView view as shown below. Ascertain that the layout is a ConstraintLayout.
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><WebViewandroid:id="@+id/webview"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>
Let’s head over to the MainActivity.kt file and write a few lines of code to show our preview:
- The first step is to locate our
WebViewusing its