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 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?


Source: Tips4all

5 comments:

  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.

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

    ReplyDelete
  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">
    <![CDATA[
    <b>Autor:</b> Mr Nice Guy<br/>
    <b>Contact:</b> myemail@grail.com<br/>
    <i>Copyright © 2011-2012 Intergalactic Spacebar Confederation </i>
    ]]>
    </string>


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

    TextView tv = (TextView) findViewById(R.id.myTextView);
    tv.setText(Html.fromHtml(getString(R.string.my_text)));


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

    Hope it's useful to some of you!

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

    Textview tv = (TextView)findViewById(R.id.textview);
    SpannableString text = new SpannableString(myString);

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

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

    ReplyDelete
  5. 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:

    myTextView.setTypeface(Typeface.SERIF,Typeface.BOLD);


    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: http://www.brighthub.com/mobile/htc/articles/74032.aspx#ixzz1FZif06dE

    ReplyDelete