androidwebviewwebviewclientwebchromeclient

How to connect webviewclient and Chromeweb view client


I already created progressbar in my web view.So It will disappear after loading web view.Now i want to add error.html page to show no internet message..I have the code but I don't know how to connect them...help me

Here is my code:

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

            mAdView = findViewById(R.id.adView);
            AdRequest adRequest = new AdRequest.Builder().build();
            mAdView.loadAd(adRequest);

            progressBar = (ProgressBar) findViewById(R.id.progressBar);
            textView = (TextView) findViewById(R.id.tvLoadingPercentage);
            //to hide progressbar after loading part 2
            liProgressContainer = (LinearLayout) findViewById(R.id.liProgressContainer);


            webView = (WebView) findViewById(R.id.webView);
            webView.setWebViewClient(new MyWebViewClient());
            WebSettings browserSetting = webView.getSettings();
            browserSetting.setJavaScriptEnabled(true);
            webView.loadUrl(url);
            webView.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress) {

                    progressBar.setProgress(progress);
                    textView.setText(progress + " %");
               }
            });
        }

 private class MyWebViewClient extends WebViewClient {


        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            liProgressContainer.setVisibility(View.VISIBLE);
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            //progressBar.setVisibility(View.VISIBLE);
            view.loadUrl(url);
            return true;
            //return super.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            liProgressContainer.setVisibility(View.GONE);

            //hide header part
        }
    }

Here is error.html code

 webView.setWebViewClient(new WebViewClient(){
            public void onRecievedError(WebView view ,int errorCode,String description ,String failingUrl)
            {
                webView.loadUrl("file:///android_asset/error.html");
            }
        });

After


Solution

  • Replace your MyWebViewClient with this code :

    private class MyWebViewClient extends WebViewClient {
    
    
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            liProgressContainer.setVisibility(View.VISIBLE);
            super.onPageStarted(view, url, favicon);
        }
    
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            //progressBar.setVisibility(View.VISIBLE);
            view.loadUrl(url);
            return true;
            //return super.shouldOverrideUrlLoading(view, url);
        }
    
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            liProgressContainer.setVisibility(View.GONE);
    
            //hide header part
        }
    
        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            view.loadUrl("file:///android_asset/error.html");
        }
    }