views:

995

answers:

4

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.

A: 

Try looking into the API demos that come along with the SDK.

You might find there what You are looking for.

Funkyidol
Thanks for your replay, but the API demos is too simple, so it's not much useful for me. I need more complex example, especially about Spanned and Spannable.
skysign
Additionally, I'm developing a kind of rich text editor, therefore I need to know more specific usage of Spanned.
skysign
Sorry mate, haven't worked with Spanned text to help you any further. Anyways, All the best
Funkyidol
Thank you for your interest.
skysign
+2  A: 

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).

andy
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-itIf you need more detail about the problem, here is another link.http://code.google.com/p/blogaway/issues/detail?id=32Probably, the link above is more informative to figure out what the problem is.
skysign
A: 

Hi all

I am working with an edit text to support the properties of bold,italic and underline.I got succeed after selecting the text and clicking on bold my text was bold.Now what my requirement is how to remove the bold again after selecting the text and clicking on bold button.

Regards, Bhavani.G

vani
+1  A: 

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.