Android ClickableSpan get text onClick()

try this:

public class LoremIpsumSpan extends ClickableSpan {
    @Override
    public void onClick(View widget) {
        // TODO add check if widget instanceof TextView
        TextView tv = (TextView) widget;
        // TODO add check if tv.getText() instanceof Spanned
        Spanned s = (Spanned) tv.getText();
        int start = s.getSpanStart(this);
        int end = s.getSpanEnd(this);
        Log.d(TAG, "onClick [" + s.subSequence(start, end) + "]");
    }
}

A little simpler, could also pass a model reference if necessary.

public class SpecialClickableSpan extends ClickableSpan {

    String text;

    public SpecialClickableSpan(String text){
         super();
         this.text = text;
    }

    @Override
    public void onClick(View widget) {
         Log.d(TAG, "onClick [" + text + "]");
    }
}

Then call new SpecialClickableSpan("My Text")


Edited: previous code was wrong, this works

    // make "dolor" (characters 12 to 17) display a toast message when touched
    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View view) {
            TextView textView = (TextView) view;
            CharSequence charSequence = textView.getText();
            if (charSequence instanceof Spannable) {
                Spannable spannableText = (Spannable)charSequence;
                ClickableSpan[] spans = spannableText.getSpans(0, textView.length(), ClickableSpan.class);
                for (ClickableSpan span : spans) {
                    int start = spannableText.getSpanStart(span);
                    int end = spannableText.getSpanEnd(span);
                    Toast.makeText(MainActivity.this, charSequence.subSequence(start, end), Toast.LENGTH_LONG).show();
                }
            }
        }
    };