How to open other links in external browser or app – WebView App

2 min


How to open other links in external browser or app - WebView App
How to open other links in external browser or app - WebView App

How to open other links in external browser or app – WebView App

When your app loads you need to open your website links in your app and other links open in external browser or apps. If you have placed ads you need to open them in external browser. And also you have to open sharing links in browser or their apps.

Did you read previous Articles?

How To Download And Install Android Studio

How to Open External links in browser

How to open other links in external browser or app – WebView App

shouldOverrideUrlLoading

We use this calling method to do this. Previously in WebViewClient we opened all the urls inside our app. So that first of all we are going to change WebViewClient to MyWebViewClient.

Old Code

myWebView.setWebViewClient(new WebViewClient());

After Changed it

myWebView.setWebViewClient(new MyWebViewClient());

Now add following code to your App. Remember using this code only your website links are opening inside the app, Other all links open in external browsers/apps.

private class MyWebViewClient extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view,String url){
if (Uri.parse(url).getHost().equals("techedutricks.com")){
//Content urls open in app
return false;

}else {
//Open external links in external browser or apps
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}

Full Code (MainActivity.java)

package com.techedutricks.techedutricks;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

private WebView myWebView;


@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings=myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

//improve WebView Performance
myWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
myWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setEnableSmoothTransition(true);

//Load Website
myWebView.loadUrl("https://www.techedutricks.com");
myWebView.setWebViewClient(new MyWebViewClient());
}

private class MyWebViewClient extends WebViewClient{
@Override
public boolean shouldOverrideUrlLoading(WebView view,String url){
if (Uri.parse(url).getHost().equals("techedutricks.com")){
//Content urls open in app
return false;

}else {
//Open external links in external browser or apps
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
}


public void onBackPressed(){
if (myWebView.canGoBack())
{
myWebView.goBack();
}
else {
super.onBackPressed();
}
}
}

Ex-: if you add “techedutricks.com“, the links matches this domain only open inside your app, other all links are opening in external browser/apps.

Now Run Your App. Share your experience with us also…


Like it? Share with your friends!

Danushka Sanjeewa
Danushka Sanjeewa is a Software engineer with more than 5 years of experience in Android App Development, Web Development, Marketing, Graphic design, Video editing, and WordPress. And also Writer/Founder of TechEduTricks.com.

One Comment

Your email address will not be published. Required fields are marked *

  1. Good post. I learn something totally new and challenging on websites I stumbleupon everyday.
    It will always be useful to read through content from other writers and
    use a little something from other websites.