Is there any example about Spanned and Spannable text

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.

Asked by: Maddie792 | Posted: 25-01-2022

Answer 1

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(,  
                      mBodyText.getSelectionStart(), mBodyText.getSelectionEnd(),  
  str.setSpan(new StyleSpan(,
              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).

Answered by: Hailey352 | Posted: 26-02-2022

Answer 2

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

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.

Answered by: Sam748 | Posted: 26-02-2022

Similar questions

Spannable of android

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 Thank you for the reply.Actually, What my need is if i select some text and clicking on Bold button(I have imp...

Android: "nowrap" in Spannable in TextView

Is it possible to have a spannable in TextView similar to this functionality in WebView? Bob McDonalds <span style="white-space: nowrap"><></span> I am trying to avoid |Email: Bob McDonalds <b. | |> | I would rather |Email: Bob McDonalds ...

android - Error in merging spannable objects

I want to merge 3 spannable objects. This code works fine: Spannable s1 = new SpannableStringBuilder("bold"); s1.setSpan(new StyleSpan(, 0, s1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); Spannable s2 = new SpannableStringBuilder("not"); Spannable s3 = new SpannableStringBuilder("BOLD"); s3.setSpan(new StyleSpan(android.gra...

textview - Android: onTouch for Spans in Spannable Text

I have a TextView containing Editable text. There are pictures in this text (drawables, I used Html.ImageGetter) for that I want to register onTouch events. So, when the user touches the image, which is part of a spannable text, I want to know that. (And, which image he touched). Is that possible? If so, maybe without dirty workarounds? Best regards, Jan Oliver Oelerich

textview - Android Custom font Spannable Typeface Span

I am using Helvetica fonts throughout my application. Currently I am creating the fonts separately from assets. So I have say these three HelveticaNeue = Typeface.createFromAsset(application.getAssets(), "fonts/HelveticaNeue.ttf"); HelveticaNeueBold = Typeface.createFromAsset(application.getAssets(), "fonts/HelveticaNeueBold.ttf"); HelveticaNeueBoldItalic = Typeface.createFromAsset(application.getAssets(), ...

android - API11 (and now12) and Spannable causing runtime NPE

Update 14th May It's the mix of text sizes that breaks it, if I replace the <item name = "android:textSize">16sp</item> with just a change of colour like <item name="android:textColor">#00ff00</item> then it runs OK. The references to TextLine,measure in the logcat should have given me a clue. I'd...

android - Get line break offset in a Spannable object

I have a Spannable object formatted like so: This is the first line. This is the second line. It's more fun than the first. Why not, lets add a third line. When I throw it in a TextView, it shows the line breaks as expected. What I want to know is, how do I get the index of the line breaks from the Spannable object? I looked at al...

textview - Android Linkify text - Spannable Text in Single Text View - As like Twitter tweet

I have a textView and text like "This is the Simple Text with KeyWord and the Link to browse" in the above text i want to make.. clicking on the Link goes to open that URL AND clicking on that KeyWord open a new Activity in my application also, There is an click event for the Whole TextView even. Ple...

java - How to set font style of selected text using custom typeface through spannable method

I want to set style to selected text from EditText using custom typeface. I am getting below error at compile time. The constructor StyleSpan(Typeface) is undefined. Below code I am applying. int start=editbox.getSelectionStart(); int end=editbox.getSelectionEnd(); Spannable span=(Spannable)editbox.getText(); StyleSpan f = new StyleSpan( ...

android - Moving EditETxt Cursor to the end of spannable text?

I have Edittext where i set spannable text into it and i want the cursor at the end of spannable text . so that user can input manuuly from key board. i have already tried ObjectOfEdietext.setSelection(ObjectOfEdietext.getText().lenght()); but it is not working for me. but if i set normal text on the Editext and do the same, it works well. Can anyone point out whats the problem??

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android