Skip to main content

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?


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

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

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.