title: Android Studio - WebView - open internal links inside WebView, external in default browser
body: <p>I am trying to add an WebView to my app, but would like to allow to open inside my app only links which are connected with my domain (www.example.com). All other links which doesn't contain the name of my domain (example.com) should be opened externaly by default browser. Using information from stockoverflow I have prepared the following code, but it still doesn't work and all links are opened inside my app, despite shouldOverrideUrlLoading is created ("if (Uri.parse(url).getHost().contains("example.com"))"). Is anyone able to help to resolve this problem? I was trying a lot of samples, but still cannot find the way how to fix it, but I fell that it can be just a small mistake in the code.</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      WebView myWebView = (WebView) findViewById(R.id.www);
      myWebView.setWebViewClient(new WebViewClient());
      WebSettings webSettings = myWebView.getSettings();
      webSettings.setJavaScriptEnabled(true);
      myWebView.loadUrl("http://www.example.com");}
  
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
      if (Uri.parse(url).getHost().contains("example.com")) {
          return false;
      }
  
      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
      startActivity(intent);
      return true;
  }
  </code></pre>
  
  <hr>
  
  <p>Resolved! I am not sure that the code is 100% correct, but works fine and maybe it will be useful for someone <em>(I am not the author, I just used a few samples from stackoverflow and put them together)</em>:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  private WebView webView;
  private ProgressDialog progress;
  
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      setContentView(R.layout.activity_main);
      WebView myWebView = (WebView) findViewById(R.id.www);
      WebSettings webSettings = myWebView.getSettings();
      webSettings.setJavaScriptEnabled(true);
      myWebView.setWebViewClient(new MyWebViewClient());
      myWebView.loadUrl("https://www.example.uk");
  }
  
  private class MyWebViewClient extends WebViewClient {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
          if (Uri.parse(url).getHost().contains("example.com")) {
              return false;
          }
          Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
          startActivity(intent);
          return true;
      }
  }
  </code></pre>