views:

7329

answers:

3

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 http://developer.android.com/guide/appendix/faq/commontasks.html#selectingtext:

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

// 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(android.graphics.Typeface.ITALIC), 0, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new BackgroundColorSpan(0xFFFFFF00), 8, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 21, str.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

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

+5  A: 

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

CommonsWare
Great... That works... Now, if I want a different size for different texts, I'm assuming it is not right to put it in the html markup because the font tag is deprecated... Is there an alternate way to do this?
Legend
Actually that gets me to another question: Is it better to have one textview with html text inside it or three text views with different markups setup without using the html class? I'm assuming its obviously the first one but just wanted to confirm it...
Legend
I have no idea what the full roster of tags that Html.fromHtml() supports -- you would need to look at the source code. Inline markup and multiple TextView widgets should be orthogonal decisions. Use multiple widgets if you need precise placement of discrete bits of text. Use inline markup if you, um, need markup inline in a widget. Remember: there is no FlowLayout in Android, so stringing together multiple TextViews to create a paragraph is not truly practical AFAIK.
CommonsWare
Thanks for that... Actually, the <small> tag worked... So I'll keep it simple and just use it...
Legend
+16  A: 

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>"));
Legend
Thanks man....!
Tom Dignan
@Tom: No problem. Glad it helped :)
Legend
+2  A: 

A really late response here but the list of supported tags is here: http://developer.android.com/guide/appendix/faq/commontasks.html#selectingtext

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

Jon
Html.fromHtml is often an easier way to style text. Also, this link is already in the original question.
AshtonBRSC