How to open other links in external browser or app – WebView App
When the 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 the browser or their apps.
Did you read previous Articles?
- How To Download And Install Android Studio?
- How To Make Android App using Android Studio?
- How to make Android WebView app – Android Studio?
- How to Remove Android App Action Bar – Android Studio?
- How to create Splash Screen? Add Welcome Screen to Your App?
- How to Improve WebView Performance 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 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 “kdapz.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…