Open a Custom Tab for links in a WebView

Published on Updated on

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));

Updated on Improve article

This site uses cookies to deliver and enhance the quality of its services and to analyze traffic. If you agree, cookies are also used to serve advertising and to personalize the content and advertisements that you see. Learn more about our use of cookies.