Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linebreaks are ignored in Html.fromHtml

I receive a text with linebreaks from an API but I can't get the linebreaks to work.

This is a part of the text I want to shown. http://pastebin.com/CLnq16mP (pasted it there because the formatting on stackoverflow wasnt correct.)

I tried this:

termsAndConditionsTextView.setText(Html.fromHtml("<html><body>" + textResponse.getText() + "</body></html>"));

and this:

termsAndConditionsTextView.setText(Html.fromHtml(textResponse.getText()));

but the linebreaks (\r\n) and spaces are always ignored.

How can I fix this?

like image 427
Robby Smet Avatar asked Jan 05 '15 15:01

Robby Smet


2 Answers

Since its html try using

</br>

You can replace all \r\n and spaces in your text by doing something like this:

    //message is your string.

    message = message.replace("\r\n","<br />");
    message = message.replace(" ","&nbsp;");
    termsAndConditionsTextView.setText(Html.fromHtml(message));

Good Luck!

like image 198
DejanRistic Avatar answered Nov 01 '22 19:11

DejanRistic


As the text is pre-formatted, even with indentation, you could use the <pre> tag.

<pre>
   your text.
</pre>

But the newlines evidently where escaped: \r\n so you still have to convert:

message = message.replace("\\r\\n", "\r\n");

The same holds for \/.

If you want to introduce bold, hyperlinks and such, then the <pre> tag fails, as it can only contain preformatted as-is text,

like image 20
Joop Eggen Avatar answered Nov 01 '22 19:11

Joop Eggen