Skip to content

WebViewClient onPageFinished() | listen for a WebView finishing loading a URL

  • by

To know about the URL loaded or not in WebView have to use the webViewClient onPageFinished() method. A WebView to have a WebViewClient to make use of the onPageFinished event callback.

You should first know about the- What is WebView in Android?

WebViewClient onPageFinished listen for a WebView finishing loading a URL

2 main callback methods of WebView Client:-

  • onPageStarted – Page loading started
  • onPageFinished – Page loading finished

Let’s build an Android WebViewClient onPageFinished() application

In the example, we used both the callback method (onPageStarted and onPageFinished) and showing text in TextView.

Step 1. Create an android project in the android studio (Follow this tutorial: Android First Program in Android Studio)

Step 2. Add code in main_activity.xml used RelativeLayout.

Adding a WebView and Textview in layout. TextView will display a message about web URLs loaded or not.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="20sp"
        android:padding="5dp"
        android:textColor="#673AB7"
        android:background="#FFEB3B"/>

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

</LinearLayout>

Step 3. Add the following code in “MainActivity.kt

package com.eyehunts.listenwebviewloadedur

import android.graphics.Bitmap
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private val url = "https://tutorial.eyehunts.com//"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        webview.loadUrl(url)

        // Set web view client
        webview.webViewClient = object : WebViewClient() {
            override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
                // Page loading started
                // Do something
                textview.setText("Page Loading Started ...")
            }

            override fun onPageFinished(view: WebView, url: String) {
                // Page loading finished
                // Enable disable back forward button
                textview.setText("Page Loading Finished ....")
            }
        }
    }
}

Note: Don’t forget to add internet permission in AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.eyehunts.listenwebviewloadedur">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

If you didn’t add internet permission, then it will throw an error or not upload the webpage in the app.

Output screenshot Android WebView Started and finished example:

WebView Started and finished

Download Link and Source code Android WebView in Github

https://github.com/EyeHunts/ListenWebViewLoadedURL

Q: How to know WebView finishing loading a URL?

Answer: To know about the WebView started loading you have to use onPageStarted() callback method.

Q: Why Android WebView onPageFinished not called?

Answer: You need to check all WebView setting and onPageFinished will not be called until all of the assets (CSS/js/images) have finished loading for that page.

Note: This example (Project) is developed in Android Studio 3.3.2. Tested on Android 9 ( Android-P), compile SDK version API 28: Android 9.0 (Pie)
MinSdkVersion=25″
TargetSdkVersion=28″Coding in Kotlin

Leave a Reply

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