Open a Custom Tab for links in a WebView

WebViews are great for seamlessly integrating your own web content into your app. When your first party content includes links to websites not owned by you, it can make sense to open these in a Custom Tab instead of the WebView. This has two benefits:

  1. The first party UX is clearly separated from the 3P web content UX.
  2. 3P web sites benefit from cookies being shared with the default browser.

To implement this, configure a custom onLoadResource handler in your WebViewClient:

WebView webView = (WebView)findViewById(;
webView.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return true;

    public void onLoadResource(WebView view, String url) {
        if (url.startsWith("")) {
            //Handle Internal Link...
        } else {
            //Open Link in a Custom Tab
            Uri uri = Uri.parse(url);
            new CustomTabsIntent.Builder()
                  .launchUrl(context, uri);