How to make Android’s WebView opens a link inside or outside current app?



Initially if we use a WebView in our Android app, if there is a link and we click it, an intent manager is being called and asking how do we want to open that link. But we can manage it to open that link inside current that WebView or regular web browser.

To do so, we need to examine what kind of link is it. In this example, I tell the WebView that if the link contains some word, for example “zofiakreasi” then please open it directly, and if not then run an intent manager so users can choose how to open that link.

browser.setWebViewClient(new WebViewClient() {
	public boolean shouldOverrideUrlLoading(WebView viewx, String urlx) {
		if(urlx.contains("zofiakreasi")){
			viewx.loadUrl(urlx);
			return false;
		}else{
			Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlx));
			startActivity(intent);
			return true;
		}
	}
	public void onReceivedError(WebView webview, int i, String s, String s1){
		webview.loadUrl("file:///android_asset/error.html");
	}
});

If you have more word to examine, you can do “else if” statement or using “or” operator inside your “if” statement.

loading...

Leave a Reply

Your email address will not be published.