Customize android webView error page

Asked

Viewed 1,241 times

5

Please have a webview on my android app, how can I customize if the user doesn’t have internet ? Thank you

Code of the current webcview :

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"); 


    }
}

  • Add the code so the guys can help you. Also enjoy and see [Ask]

1 answer

4


You didn’t mention it, but supposing you have a class Functions with a static method to check the internet connection, thus:

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 overwrite the method shouldOverrideUrlLoading 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 define in your WebView:

webView.setWebViewClient(new CustomWebViewClient());

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

  • entered the code in the question, how can I do ? Because I did not understand well .... Thank you

  • You need to create a new class that extends from WebViewClient, and this you set in the same place you did setWebViewClient, as I demonstrated in my reply. You will have a path if you have a connection and otherwise, load a HTML local reporting the lack of it.

  • i import that class through import ? how would it import ? Where is it best to create it ?

  • If you create a file, you use import, like any other. Or it can be an inner class.

  • performed all Imports but it error .... I will put the full code as this

  • You created the class CustomWebViewClient but when it comes to defining setWebViewClient you didn’t use it. See here a complete example.

  • i have kept the same file as the example but it is giving error here : if (!isDeviceOnline()) { webView.loadUrl("file:///android_asset/not-found.html");

  • I put it wrong, it’s view.loadUrl, nay webView.

  • solved, but this with getSystemService error

  • it worked, it wasn’t for this code, but it gave .... XD , I will post the code

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.