To show a custom WebView offline error page, override onReceivedError in your WebViewClient, check that the failed request is for the main frame, then hide the WebView and show your own screen with a Try again button. Keep the failed URL so the button can load it again. Without this, users see Android’s built-in “Webpage not available” page.
What Android WebView shows when the device is offline
An Android WebView with no connection shows a built-in error page that says “Webpage not available”. That page has no retry button and no branding. To a user it looks like your app broke.
You can’t restyle that page. You catch the error in the Android layer and draw your own screen over it.
What you need before you start
You need a WebView that loads your site, a WebViewClient set on it, and an error layout in the same screen. An icon, one line of text and a button is enough.
Put both in a FrameLayout so the error layout sits on top of the WebView:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/error_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:id="@+id/error_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="You're offline. Check your connection." />
<Button
android:id="@+id/retry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Try again" />
</LinearLayout>
</FrameLayout>
How to show a custom WebView offline error page, step by step
Showing a custom WebView offline error page takes three steps: catch the error, show your screen, retry the same address.
- Catch the error for the main page only. The WebViewClient reference says the current
onReceivedError“will be called for any resource (iframe, image, etc.), not just for the main page.” So filter first.isForMainFrame()“will be false for subresources or iframes,” according to the request reference.
private var failedUrl: String? = null
webView.webViewClient = object : WebViewClient() {
override fun onReceivedError(
view: WebView,
request: WebResourceRequest,
error: WebResourceError
) {
if (!request.isForMainFrame) return
failedUrl = request.url.toString()
webView.visibility = View.INVISIBLE
errorView.visibility = View.VISIBLE
}
}
-
Hide the WebView, don’t replace its content. The built-in error page still loads inside it, but an invisible WebView shows nothing.
-
Retry the address that failed.
getUrl()gives you “the URL for which the resource request was made,” so the button knows where the user was going.
retryButton.setOnClickListener {
errorView.visibility = View.GONE
webView.visibility = View.VISIBLE
failedUrl?.let { webView.loadUrl(it) }
}
If the retry fails, onReceivedError fires again and your screen comes back.
How to check the error page works
Test the offline error page on a real phone with flight mode, because that’s the case your users will hit.
- Turn on flight mode and open the app. Your screen should appear, not the built-in one.
- Turn flight mode off and tap Try again. The site should load.
- Open a working page, turn flight mode on, and tap a link. Your screen should appear, and Try again should load the page you tapped, not the home page.
- Block one image on a test page. The page should still show. If your error screen appears, the main-frame check is missing.
Why the error screen appears for a single failed image
An error screen that covers a working page means the main-frame check is missing. The older onReceivedError, the one with an error code and a string, only reported main page failures. Android deprecated it in API level 23, and the replacement reports every failed resource.
Plenty of forum answers were written for the old callback and then pasted onto the new one. One blocked ad script or missing favicon then hides your whole site.
Connection errors and HTTP errors are different callbacks
onReceivedError covers failures to reach the server, and a second callback covers pages the server refused. The reference says these errors “usually indicate inability to connect to the server.” If the server answers with an error status, Android calls onReceivedHttpError instead, which covers status codes of 400 or higher.
“You’re offline” is the wrong message when the phone is online and your server is down. For connection errors, the error reference says getErrorCode() returns one of the ERROR_* constants:
| Constant | Value | Documented meaning | A message you could show |
|---|---|---|---|
ERROR_HOST_LOOKUP |
-2 | Server or proxy hostname lookup failed | Check your internet connection |
ERROR_CONNECT |
-6 | Failed to connect to the server | Can’t reach the site right now |
ERROR_TIMEOUT |
-8 | Connection timed out | The connection is too slow |
ERROR_IO |
-7 | Failed to read or write to the server | The connection dropped |
ERROR_UNKNOWN |
-1 | Generic error | Something went wrong |
If you don’t want to write your own messages, use getDescription(). The reference says its descriptions “are localized, and thus can be used for communicating the problem to the user.”
onReceivedHttpError also fires for any resource, so apply the same main-frame check there.
Reload automatically when the connection returns
You can retry without the button by listening for the network to come back. Android’s network state guide says: “When a new network becomes the default, the app receives a call to onAvailable(Network) for the new network.”
val cm = getSystemService(ConnectivityManager::class.java)
cm.registerDefaultNetworkCallback(object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
runOnUiThread { if (errorView.isVisible) retryButton.performClick() }
}
})
The same guide says reading connectivity state this way “doesn’t require any particular permission.” Unregister the callback when the screen is destroyed.
Don’t use onLost to show the error screen early. The guide says a lost default network “might be disconnected,” which isn’t certain, and a page the user is already reading still works offline.
Native overlay or a local HTML error page?
A native overlay is the safer choice, and a local HTML page is the one most tutorials show. The usual tutorial calls loadUrl with a file from the assets folder inside the error callback. That works, but the WebView’s current page is now the error page. A reload reloads the error page, and you’ve lost the address the user wanted.
Use the overlay. It keeps the failed address, it never appears in the back history, and it can’t flash the built-in page before your HTML renders.
If you do want an HTML error page, load it through WebViewAssetLoader. Android calls web-like URLs “desirable” over file:// because they are “compatible with the Same-Origin policy.” Its default domain is appassets.androidplatform.net.
What a custom error page can’t fix
An error screen only appears when a page load fails. If the connection drops while someone is reading, nothing happens until the next tap. Requests your site makes with JavaScript aren’t main-frame loads either, so a failed search or form post inside the page never triggers the screen. Your site needs its own message for those.
It also doesn’t make anything work offline. For that, see what a WebView app does without internet and how a service worker caches pages.
What apps built with WebToAPK Free show
Apps generated with my own tool already replace the default page. The app runtime has its own error screen with a Try again button that follows your app colour. If your build is older than that runtime, rebuild the app to get it.
You can’t change that screen’s message or layout. If your content must be readable with no connection at all, package it as an offline HTML app instead, because self-contained HTML and text sections work offline.
Related guides: the loading progress bar and pull to refresh.
FAQ
How do I hide the default Webpage not available page in a WebView?
Override onReceivedError in your WebViewClient. When the failed request is for the main frame, hide the WebView and show your own error layout on top. The built-in page still loads underneath, but nobody sees it.
Why does my custom error page appear when only an image failed?
The current onReceivedError callback fires for any resource, including images and iframes. Check request.isForMainFrame() first and ignore every error where it returns false.
Does onReceivedError catch a page that returns an HTTP error?
No. A server that answers with an error status reached the device, so Android reports it through onReceivedHttpError instead. Handle that callback separately if you want your own screen for missing pages or server faults.
Do I need a permission to detect when the internet comes back?
No. Android’s network guide says NetworkCallback and the other ways of reading connectivity state don’t require any particular permission. Register a default network callback and reload when onAvailable fires.