Skip to content Skip to sidebar Skip to footer

Android Clickablespan Get Text Onclick()

I'm working on ClickableSpan in a TextView, and I'm trying to get the clicked span's text. This is my code. // this is the text we'll be operating on SpannableString text = new Spa

Solution 1:

try this:

publicclassLoremIpsumSpanextendsClickableSpan {
    @OverridepublicvoidonClick(View widget) {
        // TODO add check if widget instanceof TextViewTextViewtv= (TextView) widget;
        // TODO add check if tv.getText() instanceof SpannedSpanneds= (Spanned) tv.getText();
        intstart= s.getSpanStart(this);
        intend= s.getSpanEnd(this);
        Log.d(TAG, "onClick [" + s.subSequence(start, end) + "]");
    }
}

Solution 2:

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

publicclassSpecialClickableSpanextendsClickableSpan {

    String text;

    publicSpecialClickableSpan(String text){
         super();
         this.text = text;
    }

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

Then call new SpecialClickableSpan("My Text")

Solution 3:

Edited: previous code was wrong, this works

// make "dolor" (characters 12 to 17) display a toast message when touchedClickableSpanclickableSpan=newClickableSpan() {
        @OverridepublicvoidonClick(View view) {
            TextViewtextView= (TextView) view;
            CharSequencecharSequence= textView.getText();
            if (charSequence instanceof Spannable) {
                SpannablespannableText= (Spannable)charSequence;
                ClickableSpan[] spans = spannableText.getSpans(0, textView.length(), ClickableSpan.class);
                for (ClickableSpan span : spans) {
                    intstart= spannableText.getSpanStart(span);
                    intend= spannableText.getSpanEnd(span);
                    Toast.makeText(MainActivity.this, charSequence.subSequence(start, end), Toast.LENGTH_LONG).show();
                }
            }
        }
    };

Solution 4:

You can also use to make string spannable like this

StringhtmlLinkText="Lorem ipsum <a href='http://www.google.com'>dolor</a> sit amet";
    testView.setText(Html.fromHtml(htmlLinkText));
    testView.setMovementMethod(LinkMovementMethod.getInstance());

    CharSequencetext= testView.getText();
    if (text instanceof Spannable) {
        intend= text.length();
        Spannablesp= (Spannable) testView.getText();
        URLSpan[] urls = sp.getSpans(0, end, URLSpan.class);
        SpannableStringBuilderstyle=newSpannableStringBuilder(text);
        style.clearSpans();//should clear old spansfor (URLSpan url : urls) {
            CustomerTextClickclick=newCustomerTextClick(url.getURL());
            style.setSpan(click, sp.getSpanStart(url), sp.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        testView.setText(style);
    }

and CustomerTextClick will be

private static class CustomerTextClick extends ClickableSpan {

privateString mUrl;

    CustomerTextClick(String url) {
        mUrl = url;
    }

    @OverridepublicvoidonClick(View widget) {
        // TODO Auto-generated method stub//Toast.makeText(ctx, "hello google!",Toast.LENGTH_LONG).show();// Do your action here
    }
}

Tested and working code.

Post a Comment for "Android Clickablespan Get Text Onclick()"