Surya Teja Karra
Surya Teja Karra

Reputation: 551

Detect a blank webpage in WebView android

I was wondering if there was a way to detect blank webpages using the webview. Actually, this is a college app and so, it would not look appealing if the app loads a blank page and was thinking of using WebView.loadURL("<html><body> <h1>Oops! We have an empty page here</h1></html>") when I encounter a blank page.

So is there a way to implement this?

Btw. this is a case when the loaded URL webpage is empty and not a rendering issue or whatever it is called!

Upvotes: 3

Views: 3395

Answers (1)

MHP
MHP

Reputation: 2731

There are two possible cases:

  1. Page does not load.
  2. Page loads, but is blank.

Solutions to each case:

  1. Attach a WebViewClient to your WebView, where you override onReceivedError() to find out more about the error.
  2. Attach a WebViewClient to your WebView and in onPageFinished() use javascript to check how many child nodes <body> has. If it has no child nodes you can show your default html page.

To get number of child nodes you can use the javascriptInterface class like this:

webView.loadUrl("javascript: var count = document.body.childNodes; jsInterface.childCount(count.length);")

Upvotes: 3

Related Questions