Android Detect WebView Finished Loading URL Using Horizontal ProgressBar Show Progress into TextView

How to fill display Progress bar while loading WebView using WebChromeClient() onProgressChanged() method.

In this tutorial we would going to implement WebView with button, TextView and Horizontal ProgressBar. Now as we can see in now days every android mobile web browser application has contain Horizontal ProgressBar which will fill smoothly according to WebView done loading progress. As the WebView loads the ProgressBar starts filling automatically. We would also show the Progress in percentage format inside TextView. So here is the complete step by step tutorial for Android Detect WebView Finished Loading URL Using Horizontal ProgressBar.

android-project-download-code-button

Please add Internet permission inside AndroidManifest.xml file.

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

 How to Detect WebView Finished Loading URL Using Horizontal Progress Bar and Show Progress into TextView.

Code for MainActivity.java file.

package com.android_examples.detectwebviewloading_android_examplescom;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    WebView webView;
    TextView WebViewStatusTextView;
    Button LoadURL ;
    String URL = "http://www.google.com" ;
    ProgressBar progressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = (WebView)findViewById(R.id.webview);
        WebViewStatusTextView = (TextView)findViewById(R.id.textView);
        LoadURL = (Button)findViewById(R.id.button);
        progressBar = (ProgressBar)findViewById(R.id.progressBar1);

        LoadURL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                WebViewLoadFunction();

            }
        });
     }

     public void WebViewLoadFunction(){

         // Add setWebViewClient on WebView.
         webView.setWebViewClient(new WebViewClient(){
             @Override
             public void onPageStarted(WebView view, String url, Bitmap favicon){
                 // Do something here on page load start time.

             }
             @Override
             public void onPageFinished(WebView view, String url){
                 // Show the toast message after finishing page loading.
                 Toast.makeText(MainActivity.this,"Page Loading Finish.",Toast.LENGTH_SHORT).show();
             }
         });


        // Add setWebChromeClient on WebView.
         webView.setWebChromeClient(new WebChromeClient(){

             public void onProgressChanged(WebView webView1, int newProgress){

                 WebViewStatusTextView.setText("loading = " + newProgress + "%");
                 progressBar.setProgress(newProgress);

                 if(newProgress == 100){
                     // Page loading finish

                     WebViewStatusTextView.setText("Page Load Finish.");
                 }
             }
         });

         // Giving permissio to enable JavScript.
         webView.getSettings().setJavaScriptEnabled(true);

         // Pass the String variable which holds the website URL.
         webView.loadUrl(URL);

     }

}

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.android_examples.detectwebviewloading_android_examplescom.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="#000"
        android:textSize="25dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:gravity="center" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:minHeight="10dp"
        android:minWidth="400dp"/>

    <WebView
        android:layout_width="fill_parent"
        android:layout_height="400dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_above="@+id/button"
        android:id="@+id/webview"
        android:layout_below="@+id/progressBar1" />

    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="CLICK HERE TO LOAD URL" />

</RelativeLayout>

Code for AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android_examples.detectwebviewloading_android_examplescom">

    <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>

Screenshots :-

Android Detect WebView Finished Loading URL Using Horizontal ProgressBar Show Progress into TextView

Click here to download Android Detect WebView Finished Loading URL Using Horizontal ProgressBar Show Progress into TextView project with source code.