Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert SpannedString to Spannable

Hello I have set some text in a textview.

TextView tweet = (TextView) vi.findViewById(R.id.text);
tweet.setText(Html.fromHtml(sb.toString()));

Then I need to convert the text of the TextView into Spannble. So I did this like:

Spannable s = (Spannable) tweet.getText();

I need to convert it Spannable because I passed the TextView into a function:

    private void stripUnderlines(TextView textView) {
            Spannable s = (Spannable) textView.getText();
            URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
            for (URLSpan span : spans) {
                int start = s.getSpanStart(span);
                int end = s.getSpanEnd(span);
                s.removeSpan(span);
                span = new URLSpanNoUnderline(span.getURL());
                s.setSpan(span, start, end, 0);
            }
            textView.setText(s);
        }

private class URLSpanNoUnderline extends URLSpan {
        public URLSpanNoUnderline(String url) {
            super(url);
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(false);
        }
    }

This shows no error/warning. But throwing a Runtime Error:

java.lang.ClassCastException: android.text.SpannedString cannot be cast to android.text.Spannable

How can I convert the SpannedStringt/text of the textview into Spannble? Or can I do the same task with SpannedString inside the function?

like image 207
Kaidul Avatar asked Jul 26 '13 13:07

Kaidul


People also ask

How do you do Spannable text on Android?

To apply a span, call setSpan(Object _what_, int _start_, int _end_, int _flags_) on a Spannable object. The what parameter refers to the span to apply to the text, while the start and end parameters indicate the portion of the text to which to apply the span.

What is Spannable?

Spannable. This is the interface for text to which markup objects can be attached and detached. SpannableString. This is the class for text whose content is immutable but to which markup objects can be attached and detached.


2 Answers

How can I convert the SpannedStringt/text of the textview into Spannble?

new SpannableString(textView.getText()) should work.

Or can I do the same task with SpannedString inside the function?

Sorry, but removeSpan() and setSpan() are methods on the Spannable interface, and SpannedString does not implement Spannable.

like image 195
CommonsWare Avatar answered Oct 07 '22 17:10

CommonsWare


This should be the correct work around. Its late tho but someone might need it in the future

private void stripUnderlines(TextView textView) {
        SpannableString s = new SpannableString(textView.getText());
        URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
        for (URLSpan span : spans) {
            int start = s.getSpanStart(span);
            int end = s.getSpanEnd(span);
            s.removeSpan(span);
            span = new URLSpanNoUnderline(span.getURL());
            s.setSpan(span, start, end, 0);
        }
        textView.setText(s);
    }



private class URLSpanNoUnderline extends URLSpan {
    public URLSpanNoUnderline(String url) {
        super(url);
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        super.updateDrawState(ds);
        ds.setUnderlineText(false);
    }
}
like image 42
Divine Asiedu Avatar answered Oct 07 '22 18:10

Divine Asiedu