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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex