1

I have this statement:

s = s +   "Id: " + lc.getID() + "  Name: " + lc.getName() + "\n"
                + "  Phone Number: " + lc.getPhone() + "  Email: " + lc.getEmail() + "\n"
                + "  Description: " + lc.getDescription() + "\n\n"

that prints this out:

Id: 1  Name: Eric
 Phone Number: 8294038  Email: foo@gmail.com
 Description: Cool guy Eric

I want to Bold only the titles (Id, Name, etc).

I tried this:

        s = s + Html.fromHtml(" <b> Id: </b>" + lc.getID() + "  <b> Name: </b>" + lc.getName() + "\n"
                + "  Phone Number: " + lc.getPhone() + "  Email: " + lc.getEmail() + "\n"
                + "  Description: " + lc.getDescription() + "\n"
                + "\n\n");

But not only does it not bold, but it takes away the new lines (\n). Any ideas on how to get this done? Thanks.

Jongware
  • 22,200
  • 8
  • 54
  • 100
nick
  • 309
  • 1
  • 5
  • 15

2 Answers2

0

It will require a little bit of parsing on your end, but you definitely want to look into SpannableStrings.

For example, let's say I have the following string:

String s = "How now brown cow";

I would then turn it into a SpannableString by simply feeding the string to the constructor as follows:

SpannableString ss = new SpannableString(s);

From there you need your stylization with the spanned area. For this, I'll just use SubscriptSpan, though if you wish to make your own you can simply make your own class extending CharacterStyle and override the updateDrawState(TextPaint ds) method. The following is how you can set you span:

/ *
  * the first argument is the span effect you want, the second and third
  * are the start and end indices, respectively, and the last argument is
  * for setting a flag, which you probably won't need.
  */
ss.setSpan(new SubscriptSpan(), 0, 2, 0);

And now you can just put your string straight into the TextView and it should appear how you want, like so:

myTextView.setText(ss);
Andrew Schuster
  • 3,229
  • 2
  • 21
  • 32
0

Html.fromHtml() returns a Spanned object, designed to be put directly into a TextView or similar widget.

A Spanned is not a String.

By doing s = s + Html.fromHtml(...), you are saying "please parse this HTML into a Spanned, then throw out all the formatting to give me a String that I can concatenate onto some other String". That's not what you want -- you want to keep the formatting. But a Java String does not have formatting, and so ordinary string concatenation has no way to keep it.

Beyond that, as Manishika pointed out, newlines are ignored in HTML anyway, as you use HTML elements for vertical whitespace.

Your options include:

  1. Generate a complete HTML snippet -- including whatever it is you are trying to concatenate it to -- and then use Html.fromHtml() on the entire thing. You may wish to use a template engine (e.g., jmustache) for that, or String.format(). Or, use StringBuilder, rather than lots of + operations (less memory churn, faster performance). Be sure to use <br/> or <p> for your line breaks/paragraph delimiters.

  2. Use SpannableStringBuilder to assemble the string and its formatting from component parts.

  3. Use TextUtils.concat(s, Html.fromHtml(...)) instead of s + Html.fromHtml(...), as concat() will maintain the spans that implement the formatting. While the implementation of Spanned returned by fromHtml() is not a String, both it and String are a CharSequence, and hence work with concat().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Appreciate the answer! Don't appreciate the insults. Nonetheless, thank you for the help! – nick May 22 '14 at 19:34
  • @nick: Using `+` for multiple string concatenations is a Java anti-pattern, one that should have been covered wherever you learned Java. BTW, I added a third option to the answer (had it mind when I started the answer, then forgot about it...). – CommonsWare May 22 '14 at 19:38
  • Could you explain how else to concatenate these strings? Or at least give me a start. Excuse my lack of knowledge – nick May 22 '14 at 19:50
  • @nick: Use `StringBuilder`, as noted in the answer. http://stackoverflow.com/questions/8725739/correct-way-to-use-stringbuilder – CommonsWare May 22 '14 at 19:56