Wednesday, May 30, 2012

Is it possible to have multiple styles inside a TextView?

I was wondering if its possible to set multiple styles for different pieces of text inside a TextView. For instance, I am setting the text as follows:

descbox.setText(line1 + "\n" + line2 + "\n" + word1 + "\t" + word2 + "\t" + word3);

Now, is it possible to have a different style for each text element? I mean bold for line1, normal for word1 and so on...

I found this :

// Get our EditText object.
EditText vw = (EditText)findViewById(;

// Set the EditText's text.
vw.setText("Italic, highlighted, bold.");

// If this were just a TextView, we could do:
// vw.setText("Italic, highlighted, bold.", TextView.BufferType.SPANNABLE);
// to force it to use Spannable storage so styles can be attached.
// Or we could specify that in the XML.

// Get the EditText's internal text storage
Spannable str = vw.getText();

// Create our span sections, and assign a format to each.
str.setSpan(new StyleSpan(, 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new BackgroundColorSpan(0xFFFFFF00), 8, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new StyleSpan(, 21, str.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

But it uses position numbers inside the text. Is there a cleaner way to do this?

Source: Tips4all


  1. In case, anyone is wondering how to do this, here's one way: (Thanks to Mark again!)

    mBox = new TextView(context);
    mBox.setText(Html.fromHtml("<b>" + title + "</b>" + "<br />" +
    "<small>" + description + "</small>" + "<br />" +
    "<small>" + DateAdded + "</small>"));

    For an unofficial list of tags supported by this method, refer to this link.

  2. Try Html.fromHtml(), and mark up your text with bold and italic HTML tags (e.g., Html.fromHtml("This mixes <b>bold</b> and <i>italic</i> stuff);).

  3. Slightly off-topic, but I found this too useful not to be mentioned here.

    What if we would like to read the the Html text from string.xml resource and thus make it easy to localize. CDATA make this possible:

    <string name="my_text">
    <b>Autor:</b> Mr Nice Guy<br/>
    <i>Copyright © 2011-2012 Intergalactic Spacebar Confederation </i>

    From our Java code we could now utilize it like this:

    TextView tv = (TextView) findViewById(;

    I did not expect this to work. But it did.

    Hope it's useful to some of you!

  4. A really late response here but the list of supported tags is here:

    It also shows that Html.fromHtml isn't really needed

  5. If you dont feel like using html, you could just create a styles.xml in use it like this:

    Textview tv = (TextView)findViewById(;
    SpannableString text = new SpannableString(myString);

    text.setSpan(new TextAppearanceSpan(getContext(),,0,5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    text.setSpan(new TextAppearanceSpan(getContext(),,6,10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    tv.setText(text, TextView.BufferType.SPANNABLE);

  6. Text Appearance

    android:textStyle – Defines the style of the text. Here 3 values are allowed: bold, normal or italics.

    android:typeface – Defines the typeface of the text. Here we have 4 values: monospace, serif, sans and normal.

    In Android code it will be something like:


    Here we define not only the style of the font (bold) but the typeface too. In this case we use a “Serif” typeface.

    Read more: