Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit or create custom error page for WebView?

I have created a WebView layout, which is used to access a specific website, however it would be useful to edit or create a custom "Web page not available" resource when a handset doesn't have a network connection or the page times out. I know it's possible because if you open the app "Wikidroid" while a handset is in Airplane mode you receive "Article not available" error page instead of the standard Android "Web page not available" error page.

I have search high and low on the internet and haven't come up with any online resources which address this request. Any and all help is much appreciated. Thanks in advance.

like image 674
Sid 6x Avatar asked Nov 04 '10 21:11

Sid 6x


People also ask

How do I display HTML content in WebView?

Android App Development for BeginnersStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken web view to show html content.

What is a WebView page?

Android WebView is a system component for the Android operating system (OS) that allows Android apps to display content from the web directly inside an application.

How can I solve the error net Err_unknown_url_scheme in Android WebView?

You may get this "ERR_UNKNOWN_URL_SCHEME error" during mailto: or tel: links inside an iframe. To solve it, try to add target="_blank" in your URL Scheme/Code.


2 Answers

To determine when the device has a network connection, request the permission <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> and then you can check with the following code. First define these variables as class variables.

private Context c;
private boolean isConnected = true;

In your onCreate() method initialize c = this;

Then check for connectivity.

ConnectivityManager connectivityManager = (ConnectivityManager)
    c.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
    NetworkInfo ni = connectivityManager.getActiveNetworkInfo();
    if (ni.getState() != NetworkInfo.State.CONNECTED) {
        // record the fact that there is not connection
        isConnected = false;
    }
}

Then to intercept the WebView requets, you could do something like the following. If you use this, you will probably want to customize the error messages to include some of the information that is available in the onReceivedError method.

final String offlineMessageHtml = "DEFINE THIS";
final String timeoutMessageHtml = "DEFINE THIS";

WebView browser = (WebView) findViewById(R.id.webview);
browser.setNetworkAvailable(isConnected);
browser.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (isConnected) {
            // return false to let the WebView handle the URL
            return false;
        } else {
            // show the proper "not connected" message
            view.loadData(offlineMessageHtml, "text/html", "utf-8");
            // return true if the host application wants to leave the current 
            // WebView and handle the url itself
            return true;
        }
    }
    @Override
    public void onReceivedError (WebView view, int errorCode, 
        String description, String failingUrl) {
        if (errorCode == ERROR_TIMEOUT) {
            view.stopLoading();  // may not be needed
            view.loadData(timeoutMessageHtml, "text/html", "utf-8");
        }
    }
});
like image 198
Brian Avatar answered Dec 01 '22 01:12

Brian


Marco W. is correct.

myWebView.setWebViewClient(new WebViewClient() {
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        myWebView.loadUrl("file:///android_asset/custom_url_error.htm");

    }
});
like image 39
Cain Avatar answered Dec 01 '22 01:12

Cain