2

I've a TextView that will receive some html formatted text like:

<p class="p1"><span class="s1"><strong>Name</strong>&nbsp; Mr. A</span></p>
<p class="p1"><span class="s1"><strong>Place:</strong>&nbsp; Somewhere over the rainbow...</span></p>

I'm using this code:

CharSequence sequence = Html.fromHtml(html);
SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
text.setText(strBuilder);

Ended up with:

Name: Mr. A\n\nPlace: Somewhere over the rainbow\n\n

Problem, I don't want "\n\n" I just want one "\n", so I've done this:

sequence = sequence.toString().replaceAll("\n\n", "\n");

Ended up with:

Name: Mr. A\nPlace: Somewhere over the rainbow\n

And to remove the last "\n":

try {
    int i = sequence.toString().lastIndexOf("\n");
    sequence = new StringBuilder(sequence).replace(i, i + 1, "").toString();
} catch (Exception e) {
    e.printStackTrace();
}

Ended up with:

Name: Mr. A\nPlace: Somewhere over the rainbow

Perfect just like I wanted, but now all the HTML format is gone. How can I solve this situation?
Thanks.

EDIT 1:
User Bas van Stein suggested I should remove StringBuilder, but I didn't post all the code. So, I use a StringBuilder because if the text contains an URL I do something like this (complete code):

CharSequence sequence = Html.fromHtml(html);
sequence = sequence.toString().replaceAll("\n\n", "\n");
try {
    int i = sequence.toString().lastIndexOf("\n");
    sequence = new StringBuilder(sequence).replace(i, i + 1, "").toString();
} catch (Exception e) {
    e.printStackTrace();
}
SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
for (URLSpan span : urls) {
    makeLinkClickable(context, strBuilder, span, color);
}
text.setText(strBuilder);

makeLinkClickable method:

private void makeLinkClickable(final Context context, SpannableStringBuilder strBuilder, final URLSpan span, int color) {
    int start = strBuilder.getSpanStart(span);
    int end = strBuilder.getSpanEnd(span);
    int flags = strBuilder.getSpanFlags(span);
    strBuilder.setSpan(new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            ...
        }
    }, start, end, flags);
    strBuilder.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    strBuilder.removeSpan(span);
}

EDIT 2:
Ok so I figured out what is causing the loss of the format information, is the fact that I'm ignoring the Spanned text converted from Html.fromHtml(html). I guess I'll have to remove the "\n\n" and the last "\n" without losing the Spanned object with all the Span styles, etc. Working on it...

1
  • I'd use <br/> to replace \n. Since you want HTML line breaks, in HTML code - not Java ones. Commented Sep 25, 2015 at 15:07

1 Answer 1

3

The problem is that you use a StringBuilder, this is not needed at all. You can just set the text as html like this:

myTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));

So just remove the string builder and you should get what you want.


EDIT Ok, after your updated question, I have one more suggestion: What happens when you replace the \n before you pass it to Html.fromHtml(). Might work..

Sign up to request clarification or add additional context in comments.

2 Comments

Yes I don't have to use a StringBuilder but the problem remains. I've updated my code. Thanks
Changed my answer, not sure if it will work, but I think your on the correct way already.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.