Search code examples
javawebviewcustom-error-handling

How to show custom error message on webview


I'm trying to create custom error message for my app which is using webview. so far i'm using below code which has a very minimal problem... when i click links on the webview the page is reloading the same page instead of processing the clicked link. my links on the webview are download link so either it should download using the same app or open down-loader options

protected void fetch_web(){
    final WebView web;
    web = (WebView) findViewById(R.id.voice_mail_view_port);
    NoNetwork = (Button) findViewById(R.id.btn_no_internet);
    NoNetwork.setVisibility(View.GONE);
    String mdb = "http://192.168.23.1/default.php";

    getWindow().setFeatureInt(  Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
    final Activity MyActivity = this;
    web.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)
        {

            MyActivity.setTitle("Fetching feeds...");
            MyActivity.setProgress(progress * 100);

            loader();

            if(progress == 100) {
                MyActivity.setTitle(R.string.app_name);
                deLoader();;
            }
        }
    });
    web.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String
                failingUrl) {
            deLoader();
            alert("No Internet Connection","Let's get connected to see feeds");
            web.setVisibility(View.GONE);
            NoNetwork.setVisibility(View.VISIBLE);
        }
    });
    web.getSettings().setJavaScriptEnabled(true);
    web.loadUrl((mdb));
}
protected  void loader(){
    loading = (ProgressBar) findViewById(R.id.loader);
    loading.setVisibility(View.VISIBLE);
}
protected  void  deLoader(){
    loading = (ProgressBar) findViewById(R.id.loader);
    loading.setVisibility(View.INVISIBLE);
}

Sample download link http://192.168.23.1/download.php?id=1

Can someone help me out, i suspect my error is coming from here

web.setWebViewClient(new WebViewClient() {
    public void onReceivedError(WebView view, int errorCode, String description, String
            failingUrl) {
        deLoader();
        alert("No Internet Connection","Let's get connected to see feeds");
        web.setVisibility(View.GONE);
        NoNetwork.setVisibility(View.VISIBLE);
    }
}); 

Solution

  • Try forcing the app to open links in other browsers

    Intent intent= new Intent(Intent.ACTION_VIEW,Uri.parse(url));
    startActivity(intent);
    

    If it fails let me know to help more.