How to Replace Webpage Not Available Page To Custom Page? (WEBVIEW)

前端 未结 2 1025
再見小時候
再見小時候 2021-01-06 17:37

I want to change page from Webpage not Available or ERR_NAME_NOT_RESOLVED

to My Page \"No Internet Connection\"?

a

2条回答
  •  隐瞒了意图╮
    2021-01-06 18:10

    Replace default WebChromeClient with your custom CustomWebviewClient as below

    webView!!.setWebViewClient(object : CustomWebviewClient()
    

    then create this custom WebViewClient

        inner class CustomWebviewClient : WebViewClient() {
    
        override fun onPageStarted(view: WebView, url: String, favicon: Bitmap) {
            super.onPageStarted(view, url, favicon)
        }
    
        override fun onPageFinished(view: WebView, url: String) {
            super.onPageFinished(view, url)
        }
    
        override fun onReceivedError(view: WebView, request: WebResourceRequest, error: WebResourceError) {
            super.onReceivedError(view, request, error)
    
            if (noInternetConnection()) {
                loadNoInternetErrorView()
            } else {
                loadErrorMessageView()
            }
        }
    }
    

    whenever your WebView fails to load page it calls onReceivedError callback there you can show error layout. For error view you can create error layout within your layout with initial visiblity View.GONE and make it visible when error occurrs.

    Changes to your XML file

     
    
    
    
      
      ................
      
      //error view
    
    
    
    

    In loadErrorMessageView you can do this

    loadErrorMessageView() {
     webview.setVisibility(View.GONE);
     error_screen.setVisibility(View.VISIBLE);
     error_msg.setText("Some error Occurred");
    }
    

    Similarly you can do for NoInternet case maybe you can show "No internet message" than "Some error occurred" I hope this clears your doubt

提交回复
热议问题