Search code examples
androidwebviewloading

How to load custom error page in webview when internet not available?


I would like to make an custom html page to load when no internet connection. I don't want to use the regular one so can someone fill in what's missing? I'm pretty new to this, so I followed instructions. If anything else is needed like permission, please tell me. This is my code:

MainActivity:

package com.example.app;

public class MainActivity extends ActionBarActivity {

private WebView mWebView;

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

    mWebView = (WebView) findViewById(R.id.activity_main_webview);
        mWebView.loadUrl("http://domain.com");

    // Force links and redirects to open in the WebView instead of in a browser
    mWebView.setWebViewClient(new WebViewClient());
    // Stop local links and redirects from opening in browser instead of WebView
    mWebView.setWebViewClient(new MyAppWebViewClient());

    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    mWebView.getSettings().setLoadWithOverviewMode(true);
    mWebView.getSettings().setUseWideViewPort(true);
    mWebView.setInitialScale(1);
}

@Override
// Detect when the back button is pressed
public void onBackPressed() {
    if(mWebView.canGoBack()) {
        mWebView.goBack();
    } else {
        // Let the system handle the back button
        super.onBackPressed();
    }
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}

}

Solution

  • check whether internet is connected or not..

       private boolean haveNetworkConnection() {
          boolean haveConnectedWifi = false;
          boolean haveConnectedMobile = false;
    
          ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    
      for (NetworkInfo ni : netInfo) {
         if (ni.getTypeName().equalsIgnoreCase("WIFI"))
            if (ni.isConnected())
                haveConnectedWifi = true;
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
            if (ni.isConnected())
                haveConnectedMobile = true;
    }
    return haveConnectedWifi || haveConnectedMobile;
    }
    

    now while loading your page check the condition

        if(haveNetworkConnection()){
            mWebView.loadUrl("http://domain.com");
       } else {
        mWebView.loadUrl("file:///android_asset/custom.html");
       }
    

    add this permisson to your manifest file..

        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />