Skip to content

Instantly share code, notes, and snippets.

@Floern
Created January 9, 2016 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Floern/abdb85b4960453d672d9 to your computer and use it in GitHub Desktop.
Save Floern/abdb85b4960453d672d9 to your computer and use it in GitHub Desktop.
FixedLinkMovementMethod.java
public class FixedLinkMovementMethod extends LinkMovementMethod {
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, 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();
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);
CharacterStyle[] candidates = buffer.getSpans(off, off, CharacterStyle.class);
ClickableSpan clickableSpan = null;
for (CharacterStyle characterStyle : candidates) {
if (characterStyle.getUnderlying() instanceof ClickableSpan) {
clickableSpan = (ClickableSpan) characterStyle.getUnderlying();
break;
}
}
if (clickableSpan != null) {
if (action == MotionEvent.ACTION_UP) {
clickableSpan.onClick(widget);
}
else if (action == MotionEvent.ACTION_DOWN) {
Selection.setSelection(buffer,
buffer.getSpanStart(clickableSpan),
buffer.getSpanEnd(clickableSpan));
}
return true;
}
else {
Selection.removeSelection(buffer);
}
}
return super.onTouchEvent(widget, buffer, event);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment