Make TextView open links in CustomTabs
Nikola Despotoski
663

Better version:

@Override
public CharSequence getTransformation(CharSequence source, View view) {
if (!(view instanceof TextView)) {
return source;
}
TextView textView = (TextView) view;
if (textView.getText() == null || !(textView.getText() instanceof Spannable)) {
return source;
}
Spannable text = (Spannable) textView.getText();
URLSpan[] spans = text.getSpans(0, textView.length(), URLSpan.class);
for (URLSpan span : spans) {
int start = text.getSpanStart(span);
int end = text.getSpanEnd(span);
String url = span.getURL();

text.removeSpan(span);
text.setSpan(new URLSpanEx(url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return text;
}

You should not use the stringText variable to check for the position of the Spannable, because it could contain the same link multiple times.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.