Disk Crasher Disk Crasher -4 years ago 188
Android Question

Html.fromHTML removes double space

Maybe this isn't a bug since the documentation doesn't say much about what

exactly does, but it's a problem for me nonetheless. If the provided string contains two or more spaces in sequence,
removes all but one:

Html.fromHtml("Test 123").toString()
(java.lang.String) Test 123

If I replace the spaces with
it seems to behave as expected, but causes me grief in other parts of my program:

Html.fromHtml("Test  123").toString()
(java.lang.String) Test 123

Is this expected behavior?

Answer Source

This is how HTML normally handles rendering of whitespace.

From the HTML Spec (emphasis mine):

Note that a sequence of white spaces between words in the source document may result in an entirely different rendered inter-word spacing (except in the case of the PRE element). In particular, user agents should collapse input white space sequences when producing output inter-word space. This can and should be done even in the absence of language information

The goal of the fromHtml function is to visually render the text based on the contained HTML, so it makes sense that it will follow HTML rendering rules as closely as possible.

If you'd like to preserve white space exactly, you could see if fromHtml() supports the <pre> tag?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download