Skip to content

Instantly share code, notes, and snippets.

@deskid
Last active August 29, 2015 14:24
Show Gist options
  • Save deskid/94e504009b373a30253d to your computer and use it in GitHub Desktop.
Save deskid/94e504009b373a30253d to your computer and use it in GitHub Desktop.
WebView set default 404 page and some security setting
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");
}
}
});
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
mWebView.removeJavascriptInterface("searchBoxJavaBridge_");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment