Utsav Gupta
Utsav Gupta

Reputation: 3971

How to remove the clicked span in Android in android?

I have implemented multiple clickable spans in a TextView. My intention is to remove that paticular span which has been clicked. I am catching the onClick event of the span but it returns the whole textview. How do i isolate the span which was clicked and remove it ?

Upvotes: 0

Views: 1632

Answers (1)

Meet Vora
Meet Vora

Reputation: 2818

Maybe this will help you:

String myText = "textA";
spannableStringBuilder = new SpannableStringBuilder(myText);
spannableStringBuilder.setSpan(new ClickableSpan() {
    @Override
    public void onClick(View widget) {
        spannableStringBuilder.removeSpan(this);    // This will delete this clickable span
    }
},0,myText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
myTextView.setText(spannableStringBuilder);
myTextView.setMovementMethod(LinkMovementMethod.getInstance());

Upvotes: 2

Related Questions