How to convert SpannedString to Spannable

15,619

Solution 1

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.

Solution 2

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);
    }
}

Solution 3

Unfortunately none of these worked for me but after fiddling around with all of your solutions I found something that worked.

It will error casting the textView.getText() to Spannable unless you specify it as SPANNABLE

Also Notice from @CommonsWare's page:

Note that you do not want to call setText() on the TextView, thinking that you would be replacing the text with the modified version. You are modifying the TextView’s text in place in this fixTextView() method, and therefore setText() is not necessary. Worse, if you are using android:autoLink, setText() would cause Android go back through and add URLSpans again.

accountAddressTextView.setText(accountAddress, TextView.BufferType.SPANNABLE);

stripUnderlines(accountAddressTextView);

private void stripUnderlines(TextView textView) {
    Spannable entrySpan = (Spannable)textView.getText();
    URLSpan[] spans = entrySpan.getSpans(0, entrySpan.length(), URLSpan.class);

    for (URLSpan span: spans) {
            int start = entrySpan.getSpanStart(span);
            int end = entrySpan.getSpanEnd(span);
            entrySpan.removeSpan(span);
            span = new URLSpanNoUnderline(entrySpan.subSequence(start, end).toString());
            entrySpan.setSpan(span, start, end, 0);
    }
}
Share:
15,619

Related videos on Youtube

Kaidul
Author by

Kaidul

I believe one of my answers has brought you on my profile. Since you're here, check some of my other answers which you might find interesting as well :) 2D Segment/Quad Tree Explanation with C++ DFA construction in Knuth-Morris-Pratt algorithm Minimum number of edges to be deleted in a directed graph to remove all the cycles Count number of inversions in an array using segment trees What is the best algorithm to shuffle cards? Two elements of an array whose XOR is maximum efficiently Monolith to Microservices Raising a number to a huge exponent HashMap Space Complexity Best practices for microserviced applications

Updated on September 15, 2022

Comments

  • Kaidul
    Kaidul over 1 year

    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?

  • xiaomi
    xiaomi almost 4 years
    Into the layout, the textView can also have android:bufferType="spannable" which is convenient