Reputation: 167
i developped a webview android application, when the user is not connected to the network of course the app display this page "webpage not available" i need to add custom page (custom error image.
Help me please !
Upvotes: 0
Views: 2169
Reputation: 7371
It's actually quite simple to make this happen:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new CustomWebViewClient());
webView.loadUrl("http://google.scom");
// Inserted an error in the URL to load to test the onErrorReceived.
// You could also just remove the internet connection or remove the internet permission.
}
class CustomWebViewClient extends WebViewClient {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
view.loadData("<html>OMG! SOMETHING WENT WRONG!</html>", "", "");
}
}
}
The idea is that you supply your own custom implmentation of the WebViewClient
. In this class (CustomWebViewClient
) you override the onReceivedError
method, which is called whenever loading a URL into your WebView
fails. I haven't looked at the errorCode
at all, but you could this to differentiate between different errors.
Then when the loading fails, you simply load a static HTML page into your Webview
for instance. I wouldn't recommend loading another URL from the internet, because this would probably end in an infinite loop, as this method would be called over and over again, while the internet connection isn't available.
Upvotes: 1