I have a textview and links in it. I want the links highlighted when I touch them and also I want to open a Alertdialog when I longclick them. I tried to reach the solution by customizing URLSpan, but, since I can't access to mView before OnClick is called, I can't set the listeners:
class ConfirmSpan extends URLSpan{
View mView;
URLSpan span;
public ConfirmSpan(URLSpan span) {
super(span.getURL());
this.span = span;
//there is a nullpointerexception here since mView is null now.
mView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
((TextView)v).setBackgroundColor(0xcccccccc);
return false;
}
});
//this is also an exception
mView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Log.d("INFO","long click yay!");
return false;
}
});
}
//this should be called to reach the view. But then I can't handle touch state.
@Override
public void onClick(View widget) {
mView = widget;
//bla bla..
}
public void openURL() {
super.onClick(mView);
}
}
I think I must customize LinkMovementMethod class but how? Any comment will be appreciated. UPDATE: I am now able to handle touch event with this code:
public class CustomLinkMovementMethod extends LinkMovementMethod {
private static CustomLinkMovementMethod sInstance;
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
// Here I am changing backgorund color
if(event.getAction() == MotionEvent.ACTION_DOWN)
widget.setBackgroundColor(0xcccccccc);
if(event.getAction() == MotionEvent.ACTION_UP)
widget.setBackgroundColor(0xffffffff);
return super.onTouchEvent(widget, buffer, event);
}
public static MovementMethod getInstance() {
if (sInstance == null)
sInstance = new CustomLinkMovementMethod();
return sInstance;
}
}
But TextView named widget, a parameter OnTouchEvent(), is not what I want. It is all of the text. So, when I touch the link, text becomes gray altogether. I think I need some other methods like coloring background of the link by finding coordinates of the start and end lines of the link.
Thank you for asking this question, I think I may have a solution for you.
Check out the code in https://gist.github.com/qtyq/90f9b4894069a8b3676c
It is a factory helper class for a SpannableString which allows you to easily create a SpannableString by:
Here's an example of its usage:
And of course you can customise the methods already in that class or add your own ones if you need further customisation, just have a read of Google's documentation for SpannableString for more information on what you can do.