29

I'm struggling with using EditText and Spannable text object, These days, I've read API documents around ten times, even I'm not certain that I understand correctly. So I'm looking for a kind of example which show me how to utilize EditText and Spannable.

skysign
  • 1,084
  • 2
  • 12
  • 20
  • 5
    http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring This is a perfect answer for everyone who looking to do anything with Spannable and Textveiw. – Pinser Feb 09 '12 at 05:55
  • See [here](http://stackoverflow.com/a/41953808/3681880) and [here](http://androidcocktail.blogspot.com/2014/03/android-spannablestring-example.html) for some examples. – Suragch Jan 31 '17 at 09:51

2 Answers2

32

Since you don't specify what you can't grasp from the API it's hard to answer your questions (short answer: rewrite your question to a specific questions rather than a general one).

A typical Spannable-example is something like this to turn selected text in an EditText into Italic:

Spannable str = mBodyText.getText(); 
if(mBodyText.getSelectionEnd() > mBodyText.getSelectionStart()) 
  str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC),  
                      mBodyText.getSelectionStart(), mBodyText.getSelectionEnd(),  
                      Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
else
  str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC),
              mBodyText.getSelectionEnd(),
              mBodyText.getSelectionStart(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

This is cut and pasted from something else, so your direct-pastability might have suffered, but it at least shows a working example of a Spannable (in this case a StyleSpan). In the API you can find the other types of Spans (notably ImageSpan, which is a common questions among newly converted droiders).

Macke
  • 24,812
  • 7
  • 82
  • 118
andy
  • 696
  • 5
  • 14
  • This reply is very helpful for me, Thanks. But I still have a bunch of problem, I have to solve. As you described above, here is a link to show the problem what I've faced on. http://stackoverflow.com/questions/2093111/when-some-text-is-copied-and-pasted-pasted-text-inherit-previous-text-style-it If you need more detail about the problem, here is another link. http://code.google.com/p/blogaway/issues/detail?id=32 Probably, the link above is more informative to figure out what the problem is. – skysign Feb 02 '10 at 08:50
  • 1
    While the purpose of `SpannableString` is quite obvious, an example of `SpannedString` would be very helpful, as it is totally unclear to me what to use it for and how to create it in the first place. – AndreKR Mar 08 '15 at 10:40
10

I'm just starting to try to figure it out too, and it seems unnecessarily tricky.

Here's a working method to add NEW spannable text to an existing view. I wanted to add colored text to a view, and this seemed like the only way to do it.

Though it feels like an ugly hack, you can create a dummy TextView (not shown anywhere) and style the text there, then append that styled text to wherever you want. Credit for it goes to iifuzz at anddev.org. My code looks like so:

    spanbuffer = new TextView(context);
    spanbuffer.setText(newText, TextView.BufferType.SPANNABLE);
    Spannable s = (Spannable) spanbuffer.getText();
    s.setSpan(new ForegroundColorSpan(Color.RED), 0, newText.length() - 1, 0);
    this.append(s);

I think you're supposed to be able to create new spannable text using the SpannableFactory, like so:

    Spannable s = Spannable.Factory.getInstance().newSpannable(newText);

but I couldn't get this text to actually show new span effects, so I gave up.

Sam L.
  • 166
  • 2
  • 8