Detecting Webview Error and Show Message

You're most of the way there... Just implement onReceivedError and handle the errors that you want.


All answer above are deprecated. You should use this code after on Page finished

 @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){
           //Your code to do
        Toast.makeText(getActivity(), "Your Internet Connection May not be active Or " + error.getDescription(), Toast.LENGTH_LONG).show();
    }

Add this after onpagefinished :

    public void onReceivedError(WebView view, int errorCod,String description, String failingUrl) {
            Toast.makeText(Webform.this, "Your Internet Connection May not be active Or " + description , Toast.LENGTH_LONG).show();
        }

Don't forget to import android.widget.Toast;


Updated answer as per API 23 Marshmallow

WebViewClient Errors Handling

    /*
     * Added in API level 23 replacing :-
     *
     * onReceivedError(WebView view, int errorCode, String description, String failingUrl) 
    */
    @Override
    public void onReceivedError(WebView view, WebResourceRequest request,
            WebResourceError error) {

        Toast.makeText(getActivity(),
                "WebView Error" + error.getDescription(),
                Toast.LENGTH_SHORT).show();

        super.onReceivedError(view, request, error);

    }

    /*
      Added in API level 23
    */
    @Override
    public void onReceivedHttpError(WebView view,
            WebResourceRequest request, WebResourceResponse errorResponse) {

        Toast.makeText(getActivity(),
                "WebView Error" + errorResponse.getReasonPhrase(),
                Toast.LENGTH_SHORT).show();


        super.onReceivedHttpError(view, request, errorResponse);
    }

Network Error Handling

        webView.loadUrl(urlToLoad);

        if (!isConnected(getActivity())) {
            Toast.makeText(getActivity(), "You are offline ", Toast.LENGTH_SHORT).show();

        }

     /**
     * Check if there is any connectivity
     * 
     * @param context
     * @return is Device Connected
     */
    public static boolean isConnected(Context context) {

        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (null != cm) {
            NetworkInfo info = cm.getActiveNetworkInfo();

            return (info != null && info.isConnected());
        }
        return false;
    }