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

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.

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

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

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.