Reputation: 9010
I used Android.text.style.ClickableSpan
to make a part (Black
) of a string (Blue | Black
) clickable:
SpannableString spannableString = new SpannableString("Blue | Black ");
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
//...
}
};
ss.setSpan(clickableSpan, 7, 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView textView = (TextView) findViewById(R.id.secondActivity_textView4);
textView.setText(spannableString);
textView.setMovementMethod(LinkMovementMethod.getInstance());
So Black
part of the string is clickable. What I want is that when the user clicks Black
, it should make Black
Not-clickable, and Blue
(another part of the same string) clickable.
So to make Blue
clickable, we can call setSpan()
on the same spannableString
another time. But how can I make Black
not-clickable?
Upvotes: 3
Views: 779
Reputation: 55340
You can call removeSpan()
to remove any previously added Spans. In this particular case it's very easy, as we hold a reference to the very Span we want to remove:
ClickableSpan clickableSpan = new ClickableSpan()
{
@Override
public void onClick(View view)
{
((SpannableString)textView.getText()).removeSpan(this);
}
};
Another option could be to iterate over all ClickableSpan
instances and remove them all, such as:
SpannableString str = (SpannableString)textView.getText();
for (ClickableSpan span : str.getSpans(0, str.length(), ClickableSpan.class))
str.removeSpan(span);
For some reason that I cannot fathom, the documentation for spans is really poor... they are quite powerful!
Upvotes: 2