API: public boolean onTouch(View v, MotionEvent event) warning: 'onTouch' should call 'View#performClick' when a click is detected
public static void setTextWithLinks(TextView textView, CharSequence html) {
textView.setText(html);
// TODO https://code.google.com/p/android/issues/detail?id=191430
//noinspection Convert2Lambda
textView.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_UP ||
action == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
TextView widget = (TextView) v;
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
ClickableSpan[] links = Spannable.Factory.getInstance()
.newSpannable(widget.getText())
.getSpans(off, off, ClickableSpan.class);
if (links.length != 0) {
if (action == MotionEvent.ACTION_UP) {
if (links[0] instanceof URLSpan) {
openWebUrlExternal(widget.getContext(), null,
((URLSpan) links[0]).getURL(), null);
} else {
links[0].onClick(widget);
}
}
return true;
}
}
return false;
}
});
}