Reputation: 15855
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?
Upvotes: 19
Views: 17528
Reputation: 197
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);
}
}
Upvotes: 1
Reputation: 71
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);
}
}
Upvotes: 2
Reputation: 28171
If you specify BufferType.SPANNABLE
when setting the TextView
's text, then when getting the text you can cast it to Spannable
myTextView.setText("hello", TextView.BufferType.SPANNABLE);
...
...
...
Spannable str = (Spannable) myTextView.getText();
Upvotes: 0
Reputation: 1006539
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
.
Upvotes: 24