Search code examples
javaandroidwebviewandroid-webview

How to prevent url to show in web view


Hi there I am trying to create app where I am using web view to show my google drive folder but whenever internet got disconnect in between loading the URL at that a msg comes with showing the URL information. What can I do to prevent showing URL from users. Is it possible to show some other msg whenever internet goes down on starting or when it goes down on after starting loading.


Solution

  • I have a custom HTML page that will show if something wrong happened when loading the url.

    webView.setWebViewClient(new WebViewClient(){
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            switch(errorCode){
                case ERROR_HOST_LOOKUP:
                    webView.loadDataWithBaseURL(null,"<YOUR OWN CUSTOM HTML PAGE TO SHOW WHEN THERE'S AN ERROR>", "text/html", "UTF-8",null);
                    break;
                case ERROR_CONNECT:
                    webView.loadDataWithBaseURL(null,"<YOUR OWN CUSTOM HTML PAGE TO SHOW WHEN THERE'S AN ERROR>", "text/html", "UTF-8",null);
                    break;
                case ...[IF YOU WANT TO CATCH MORE ERRORS]
            }
        }
    }
    

    Reference: https://developer.android.com/reference/android/webkit/WebViewClient#onReceivedError(android.webkit.WebView,%20int,%20java.lang.String,%20java.lang.String)

    Error Code Reference: https://developer.android.com/reference/android/webkit/WebViewClient#ERROR_AUTHENTICATION