Well I had certain issues with LinkMovement for example the click is triggered on MotionEvent.ActionDown
itself which is not really a click so I ended making a custom MovementMethod
class ClickMovementMethod implements MovementMethod {
private final int normalColor, selectedColor;
private float[] pressedCoordinate = null;
private ClickMovementMethod(int normalColor, int selectedColor) {
this.normalColor = normalColor;
this.selectedColor = selectedColor;
}
@Override
public void initialize(TextView widget, Spannable text) {
}
@Override
public boolean onKeyDown(TextView widget, Spannable text, int keyCode, KeyEvent event) {
return false;
}
@Override
public boolean onKeyUp(TextView widget, Spannable text, int keyCode, KeyEvent event) {
return false;
}
@Override
public boolean onKeyOther(TextView view, Spannable text, KeyEvent event) {
return false;
}
@Override
public void onTakeFocus(TextView widget, Spannable text, int direction) {
}
@Override
public boolean onTrackballEvent(TextView widget, Spannable text, MotionEvent event) {
return false;
}
@Override
public boolean onTouchEvent(TextView widget, Spannable text, MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_UP) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
ClickableSpan[] links = text.getSpans(off, off, ClickableSpan.class);
if (action == MotionEvent.ACTION_UP) {
if (pressedCoordinate != null) {
if (Math.abs(pressedCoordinate[0] - event.getX()) < 10 &&
Math.abs(pressedCoordinate[1] - event.getY()) < 10) {
widget.setLinkTextColor(normalColor);
links[0].onClick(widget);
pressedCoordinate = null;
} else {
widget.setLinkTextColor(normalColor);
pressedCoordinate = null;
}
}
} else if (links.length > 0) {
widget.setLinkTextColor(selectedColor);
pressedCoordinate = new float[]{event.getX(), event.getY()};
}
}
return false;
}
@Override
public boolean onGenericMotionEvent(TextView widget, Spannable text, MotionEvent event) {
return false;
}
@Override
public boolean canSelectArbitrarily() {
return false;
}
}
tv
is of type EditText, true you can click on the span but not edit this as normal. – Whitsunday