Created
January 9, 2016 20:38
-
-
Save Floern/abdb85b4960453d672d9 to your computer and use it in GitHub Desktop.
FixedLinkMovementMethod.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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