I have TextView with spans of type ClickableStringSpan defined as below:
public class ClickableStringSpan extends ClickableSpan {
private View.OnClickListener mListener;
int color;
public ClickableStringSpan(View.OnClickListener listener,int color) {
mListener = listener;
this.color = color;
}
@Override
public void onClick(View v) {
mListener.onClick(v);
}
@Override public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
ds.setColor(color);
}
}
I set clickable spans on my text like this:
spanStr.setSpan(new ClickableString(new linkedTextClickListener(), linkColor),
startIndex, endIndex,
SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
Now I want to apply these string to EditTexts instead of TextViews. Everything is fine just Clickable strings are now not clicked anymore. I want to know how can I pass clicks on this sort of spans to their assigned clicklistener?
Update: My main concern to edit text is I want to allow user select some part of text and share it meanwhile he/she can click on ClickableSpans.
You need to set the TextView's/EditText's movement method to LinkMovementMethod to be able to get clicked links. Unfortunately that disables the ability to select text which works only if you set the movement method to ArrowKeyMovementMethod. http://developer.android.com/reference/android/text/method/LinkMovementMethod.html http://developer.android.com/reference/android/text/method/ArrowKeyMovementMethod.html
To solve this I created an custom MovementMethod class that inherits from ArrowKeyMovementMethod and adds the ability to click links. :
All you need to do is set the movement method of your EditText and you're good to go:
The code above only works with unformatted text, meaning once you decide to format your text using text styles (bold, italic etc.) or different font sizes, it won't find the clicked link any more. I do have the code to deal with formatted text but since that wasn't part of the question I made the sample code as short as possible.
The following code example should work for you, I have also tested and it gives you click events of
ClickableSpanString
May be you have forgot to add
setMovementMethod