Android: Make a TextView part of the text NOT-clickable at runtime?
I used Android.text.style.ClickableSpan
to make part ( Black
) of line ( 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 line is clickable. I want that when the user clicks Black
it should make it Black
Not-clickable and Blue
(the other part of the same line) clickable.
So, to make it Blue
available, we can call setSpan()
at the same time spannableString
at a different time. But how can I make it Black
non-clickable?
source to share
You can call removeSpan()
to remove previously added gaps. In this particular case, it is very easy, since we are referring to the very interval that 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 the instances ClickableSpan
and remove them all, for example:
SpannableString str = (SpannableString)textView.getText();
for (ClickableSpan span : str.getSpans(0, str.length(), ClickableSpan.class))
str.removeSpan(span);
For some reason that I can't figure out, the documentation for spans is really poor ... they're pretty powerful!
source to share