Sunday, March 20, 2016

android - Detect when WebView finish loading a URL

MainActivity.java

package com.cfsuman.androidtutorials;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {
        private MainActivity mContext;
        private WebView mWebView;
        private TextView mTextView;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                // Get the context
                mContext = this;

                // Get the widgets reference from XML layout
                mWebView = findViewById(R.id.webView);
                mTextView = findViewById(R.id.textView);
                Button button = findViewById(R.id.button);


                // Set a click listener for button widget
                button.setOnClickListener(view -> {
                        // Request to render the web page
                        renderWebPage("https://www.android--code.com/?m=1");
                });
        }



        // Custom method to render a web page
        protected void renderWebPage(String urlToRender){
                // Set a WebViewClient for WebView
                mWebView.setWebViewClient(new WebViewClient(){
                        @Override
                        public void onPageStarted(WebView view, String url,
                                                  Bitmap favicon){
                                // Page loading started
                                // Do something
                        }


                        @Override
                        public void onPageFinished(WebView view, String url){
                                // Page loading finished
                                showToast("Page fully loaded");
                        }
                });


                // Set a WebChromeClient for WebView
                // Another way to determine when page loading finish
                mWebView.setWebChromeClient(new WebChromeClient(){
                        public void onProgressChanged(
                                WebView view, int newProgress){

                                // Show the loading progress on text view
                                mTextView.setText("Page loading : "
                                        + newProgress + "%");

                                if(newProgress == 100){
                                        // Page loading finish
                                        mTextView.setText(
                                                "Page fully loaded.");
                                }
                        }
                });


                // Enable JavaScript
                mWebView.getSettings().setJavaScriptEnabled(true);

                // Load the url in the WebView
                mWebView.loadUrl(urlToRender);
        }



        // Method to show toast message
        private void showToast(String message){
                Toast.makeText(mContext,message,Toast.LENGTH_SHORT).show();
        }
}
activity_main.xml

<?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"
    android:background="#DCDCDC">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:text="Load URL"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:fontFamily="sans-serif"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="@+id/button"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toTopOf="@+id/button"
        tools:text="TextView" />

    <WebView
        android:id="@+id/webView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>