Search code examples
androidspannablestringclickablespan

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 ?


Solution

  • 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());