I am trying to load the URL to webView on Android Studio. I load the URL based on user input (in my code below is urlResult). Sometimes the user can input in the form of "https://exampleweb.com", or "https://www.exampleweb.com", or "www.exampleweb.com", or "exampleweb.com". Sometimes, the URL that is entered by the user may not load anything (either the link is not active, blocked, does not exist, etc.). When this happens, I would like to show an error HTML page that I already created locally (errorpage.html). How can I achieve this? Thank you!
Here is my code:
public class WebResult extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_result);
WebView webView = findViewById(R.id.webView_UrlResult);
webView.setWebViewClient(new WebViewClient(){
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
webView.loadUrl("file:///android_asset/errorpage.html");
}
});
if (urlResult.startsWith("https://")) {
webView.loadUrl(urlResult);
} else {
webView.loadUrl("https://" + urlResult);
}
}
}