title: Webview - Open links in external browser?
body: <p>Can anyone help me in opening specific link in External browser or other android app?</p>
  
  <p>For Example if URL matches <strong>whatsapp://</strong> then it should open link in WhatsApp app and if WhatsApp is not installed then it should open <strong><a href="https://play.google.com/store/apps/details?id=com.whatsapp" rel="nofollow noreferrer">https://play.google.com/store/apps/details?id=com.whatsapp</a></strong> in android browser.</p>
  
  <p>FYI: I am using <a href="https://github.com/SurfEdge/flarum-mobile-android" rel="nofollow noreferrer">https://github.com/SurfEdge/flarum-mobile-android</a> to create Web View App for my Forum website.</p>
  
  <p>My Android code looks like:</p>
  
  <pre><code>package com.example.example;
  
  import android.content.Intent;
  import android.graphics.Bitmap;
  import android.os.Build;
  import android.os.Bundle;
  import android.os.Message;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.KeyEvent;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.webkit.CookieManager;
  import android.webkit.WebChromeClient;
  import android.webkit.WebSettings;
  import android.webkit.WebView;
  import android.webkit.WebViewClient;
  import android.widget.ProgressBar;
  
  import im.delight.android.webview.AdvancedWebView;
  
  public class MainActivity extends AppCompatActivity implements AdvancedWebView.Listener{
  
  public AdvancedWebView webView;
  private ProgressBar mPbar = null;
  
  private static final String  url = "https://example.com/";
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
      mPbar = (ProgressBar) findViewById(R.id.loader);
  
      webView = (AdvancedWebView) findViewById(R.id.newWeb);
      webView.loadUrl(url);
      webView.setListener(this, this);
  
      WebSettings webSettings = webView.getSettings();
      webSettings.setJavaScriptEnabled(true);
      webSettings.setAppCacheEnabled(true);
      webSettings.setDomStorageEnabled(true);
      webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
      webSettings.setAllowFileAccess(true);
  
      CookieManager cookieManager = CookieManager.getInstance();
      cookieManager.setAcceptCookie(true);
  
      webView.setWebChromeClient(new WebChromeClient() {
          @Override
          public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
              AdvancedWebView newWebView = new AdvancedWebView(MainActivity.this);
              WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
              transport.setWebView(newWebView);
              resultMsg.sendToTarget();
              return true;
          }
      });
  
      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.KITKAT) {
          webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
          webSettings.setAllowUniversalAccessFromFileURLs(true);
      } else {
          webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
      }
  
      webView.setWebViewClient(new WebViewClient(){
      @Override
      public void onPageStarted(WebView view, String url, Bitmap favicon) {
      mPbar.setVisibility(View.VISIBLE);
      }
  
      public void onPageFinished(WebView view, String url) {
      mPbar.setVisibility(View.GONE);
      }
  
      @SuppressWarnings("deprecation")
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
          if (url.startsWith("whatsapp://")) {
              final Uri uri = Uri.parse("market://details?id=com.whatsapp");
              final Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
              @SuppressWarnings({"NewApi", "deprecation"})
              final int newDocumentFlag = (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) ? Intent.FLAG_ACTIVITY_NEW_DOCUMENT : Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET;
              goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | newDocumentFlag | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
  
          try {
              startActivity(goToMarket);
          } catch (ActivityNotFoundException ex) {
              startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=com.whatsapp")));
          }
          return true;
      }
  
      return super.shouldOverrideUrlLoading(view, url);
      }
  
      });
  
  
      webView.setOnKeyListener( new View.OnKeyListener() {
          @Override
          public boolean onKey( View v, int keyCode, KeyEvent event ) {
              if ((keyCode == KeyEvent.KEYCODE_BACK) &amp;&amp; webView.canGoBack()) {
                  webView.goBack();
                  return true;
              }
              return false;
          }
      });
  
      webView.setThirdPartyCookiesEnabled(true);
      webView.setCookiesEnabled(true);
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
  
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
          return true;
      }
  
      return super.onOptionsItemSelected(item);
  }
  
  
  @Override
  protected void onResume() {
      super.onResume();
      webView.onResume();
  }
  
  @Override
  protected void onPause() {
      webView.onPause();
      super.onPause();
  }
  
  @Override
  protected void onDestroy() {
      webView.onDestroy();
      super.onDestroy();
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
      super.onActivityResult(requestCode, resultCode, intent);
      webView.onActivityResult(requestCode, resultCode, intent);
  }
  
  @Override
  public void onBackPressed() {
      if (!webView.onBackPressed()) { return; }
      super.onBackPressed();
  }
  
  
  @Override
  public void onPageStarted(String url, Bitmap favicon) {
  
  }
  
  @Override
  public void onPageFinished(String url) {
  
  }
  
  @Override
  public void onPageError(int errorCode, String description, String failingUrl) {
  
  }
  
  @Override
  public void onDownloadRequested(String url, String suggestedFilename, String mimeType, long contentLength, String contentDisposition, String userAgent) {
  
  }
  
  @Override
  public void onExternalPageRequest(String url) {
  
  }
  }
  </code></pre>
  
  <p><strong>Error:</strong></p>
  
  <pre><code>Error:(86, 11) error: cannot find symbol class Uri
  Error:(86, 21) error: cannot find symbol variable Uri
  Error:(94, 22) error: cannot find symbol class ActivityNotFoundException
  Error:(95, 62) error: cannot find symbol variable Uri
  </code></pre>
  
  <p>P.S: I am not an Android Developer</p>