Can a TextView be selectable AND contain links?
android, textview
Solution
I figured it out. You need to subclass LinkMovementMethod and add support for text selection. It's really unfortunate that it doesn't support it natively. I just overrode the relevant methods using the equivalent ones from the source code for `ArrowKeyMovementMethod`. I guess that's one benefit of Android being open source!
public class CustomMovementMethod extends LinkMovementMethod {
@Override
public boolean canSelectArbitrarily () {
return true;
}
@Override
public void initialize(TextView widget, Spannable text) {
Selection.setSelection(text, text.length());
}
@Override
public void onTakeFocus(TextView view, Spannable text, int dir) {
if ((dir & (View.FOCUS_FORWARD | View.FOCUS_DOWN)) != 0) {
if (view.getLayout() == null) {
// This shouldn't be null, but do something sensible if it is.
Selection.setSelection(text, text.length());
}
} else {
Selection.setSelection(text, text.length());
}
}
}
To use it, just instantiate it directly, like so:
textView.setMovementMethod(new CustomMovementMethod());
Problem
I've run into a problem with `TextView`. I can make it selectable using `setTextIsSelectable(true)`, but when I enable links to be clicked via `setMovementMethod(LinkMovementMethod.getInstance())`, it is no longer selectable. Please note, I don't mean making raw links clickable, but rather making actual words clickable by loading the `TextView` with HTML markup using something like `setText(Html.fromHtml("<a href='http://stackoverflow.com'>Hello World!</a>"))`.