Customize webView error page android

4

Please have a webview in my android app, how can i customize it if the user does not have internet? Thanks

Current webcview code:

WebView wv=(WebView) findViewById(R.id.webView); 
        WebSettings ws = wv.getSettings(); 
        ws.setJavaScriptEnabled(true); 
        ws.setSupportZoom(true); 
        wv.setWebViewClient(new WebViewClient());
        wv.loadUrl("http://google.com"); 

Code suggested by @PauloRodrigues:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;  
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.net.NetworkInfo;
import android.content.Context;
import android.net.ConnectivityManager;

private class CustomWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (!Functions.isDeviceOnline(context)) {
            view.loadUrl("file:///android_asset/no-internet.html");
        } else {
            view.loadUrl(url);
        }

        return true;
    }
}


public class ConectActivity extends Activity { 

    public static boolean isDeviceOnline(Context pContext) {
        ConnectivityManager cm = (ConnectivityManager) pContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();

        return (netInfo != null && netInfo.isConnectedOrConnecting());
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_conect);
       
        
        WebView wv=(WebView) findViewById(R.id.webView); 
        WebSettings ws = wv.getSettings(); 
        ws.setJavaScriptEnabled(true); 
        ws.setSupportZoom(true); // *** Permite o zoom no webview - Augusto Furlan ***
        wv.setWebViewClient(new WebViewClient());
        wv.loadUrl("http://www.google.com"); 


    }
}
    
asked by anonymous 23.10.2015 / 12:55

1 answer

3

You did not mention it, but assuming you have a Functions class with a static method to check the internet connection, like this:

public static boolean isDeviceOnline(Context pContext) {
    ConnectivityManager cm = (ConnectivityManager) pContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();

    return (netInfo != null && netInfo.isConnectedOrConnecting());
}

And then, you will need to override the shouldOverrideUrlLoading method of your WebViewClient :

private class CustomWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (!Functions.isDeviceOnline(context)) {
            view.loadUrl("file:///android_asset/no-internet.html");
        } else {
            view.loadUrl(url);
        }

        return true;
    }
}

And set in your WebView :

webView.setWebViewClient(new CustomWebViewClient());

The no-internet.html file is your custom HTML in case there is no internet connection. This file you place in the assets directory of your project.

    
23.10.2015 / 13:23