Sunday, March 20, 2016

android - Detect when WebView finish loading a URL

activity_main.xml

<?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:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    android:background="#e8eae5"
    >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#000000"
        android:padding="5dp"
        />
    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tv"
        />
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load URL"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
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.WebViewClient;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.webkit.WebView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    private Context mContext;
    private Activity mActivity;

    private RelativeLayout mRelativeLayout;
    private TextView mTextView;
    private Button mButton;
    private WebView mWebView;
    private String mURL;

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

        // Get the application context
        mContext = getApplicationContext();

        // Get the activity
        mActivity = MainActivity.this;

        // Specify the url to surf
        mURL = "http://www.google.com";

        // Get the widgets reference from XML layout
        mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mTextView = (TextView) findViewById(R.id.tv);
        mButton = (Button) findViewById(R.id.btn);
        mWebView = (WebView) findViewById(R.id.web_view);

        // Set a click listener for button widget
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Set a WebViewClient for WebView
                mWebView.setWebViewClient(new WebViewClient(){
                    @Override
                    public void onPageStarted(WebView view, String url, Bitmap favicon){
                        // Page loading started
                        // Do something
                    }

                    /*
                        public void onPageFinished (WebView view, String url)
                            Notify the host application that a page has finished loading. This
                            method is called only for main frame. When onPageFinished() is called,
                            the rendering picture may not be updated yet. To get the notification
                            for the new Picture, use onNewPicture(WebView, Picture).

                        Parameters
                            view WebView: The WebView that is initiating the callback.
                            url String: The url of the page.
                    */
                    @Override
                    public void onPageFinished(WebView view, String url){
                        // Page loading finished
                        Toast.makeText(mContext,"Page Loaded.",Toast.LENGTH_SHORT).show();
                    }
                });

                // Set a WebChromeClient for WebView
                // Another way to determine when page loading finish
                mWebView.setWebChromeClient(new WebChromeClient(){
                    /*
                        public void onProgressChanged (WebView view, int newProgress)
                            Tell the host application the current progress of loading a page.

                        Parameters
                            view WebView: The WebView that initiated the callback.

                            newProgress int: Current page loading progress, represented by an
                                integer between 0 and 100.
                    */
                    public void onProgressChanged(WebView view, int newProgress){
                        mTextView.setText("Page loading : " + newProgress + "%");

                        if(newProgress == 100){
                            // Page loading finish
                            mTextView.setText("Page Loaded.");
                        }
                    }
                });
                // Enable JavaScript
                mWebView.getSettings().setJavaScriptEnabled(true);

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