androidgoogle-chromeandroid-customtabs

Chrome custom tabs and textview


I have a text view with a link inside it. In code I call the setMovementMethod to open the link when the user clicks on the text. But it opens it in the default browser or the browser chooser.

How can I use chrome custom tabs with a clickable textview?


Solution

  • This is because the TextView creates URLSpan which is ClickableSpan for each link text pattern. Once the MovementMethod finds url it calls onClick method of the URLSpan. This event starts the ACTION_VIEW intent, that's why you see the default browser starting instead.

    What you could do is write your own implementation of the URLSpan, where you'd override onClick method and start the CustomTabs service from there.

    First create custom URLSpan that will override onClick method:

    public class CustomTabsURLSpan extends URLSpan {
        public CustomTabsURLSpan(String url) {
            super(url);
        }
    
        public CustomTabsURLSpan(Parcel src) {
            super(src);
        }
    
        @Override
        public void onClick(View widget) {
           String url = getUrl();
           //attempt to open in CustomTabs, if that fails call super.onClick(widget);
        }
    }
    

    Create custom transformation method, that will set spans to the links:

    public class LinkTransformationMethod implements TransformationMethod {
    
        @Override
        public CharSequence getTransformation(CharSequence source, View view) {
            if (view instanceof TextView) {
                TextView textView = (TextView) view;
                Linkify.addLinks(textView, Linkify.WEB_URLS);
                String stringText = textView.getText().toString();
                Spannable text = (Spannable) textView.getText();
                URLSpan[] spans = text.getSpans(0, textView.length(), URLSpan.class);
                for (int i = spans.length - 1; i >= 0; i--) {
                    URLSpan oldSpan = spans[i];
                    text.removeSpan(oldSpan);
                    String url = oldSpan.getURL();
                    int startIndex = stringText.indexOf(url);
                    int lastIndex = startIndex + url.length();
                    text.setSpan(new CustomTabsURLSpan(url), startIndex, lastIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
                return text;
            }
            return source;
        }
    
        @Override
        public void onFocusChanged(View view, CharSequence sourceText, boolean focused, int direction, Rect previouslyFocusedRect) {
    
        }
    }
    

    And here is quick explanation: https://medium.com/@nullthemall/make-textview-open-links-in-customtabs-12fdcf4bb684#.ig1chpbbe