3

This is in relation to a previous question I asked here. Calling replaceAll("\n", "<br />") and then Html.fromHtml() will properly format the text in pre-Ice Cream Sandwich; however, as you can see from the photo it does not work in ICS. I've tried at least thirty different ways to capture and replace the line breaks in the text, but I come up empty handed. Is there something in particular I need to call in ICS, is this a bug, has anyone else experienced this problem? Can anyone think of a work around, because there's got to be one. Also, can anyone offer some insight as to why this may be happening? It's very peculiar.

To be clear: I've tested this on Froyo and Gingerbread and the text formatted properly. In Ice Cream Sandwich, it does not.

Here's an example of the text being returned from Last.fm. Scroll to the bottom, it starts at "content". Bon Iver URL

Here's a demo app if you're interested in testing it first hand. http://dl.dropbox.com/u/2301775/lastfm-api-test.zip

Here are two screenshots illustrating what I mean. They are from the test app. The first running Gingerbread and the second running Ice Cream Sandwich.

Gingerbread screenshot

Ice Cream Sandwich screenshot

Community
  • 1
  • 1
adneal
  • 30,484
  • 10
  • 122
  • 151
  • Have you tried dumping out the source text to confirm that the replacement is happening? – Lawrence D'Oliveiro Feb 07 '12 at 11:53
  • Odd, so after a quick test, ICS worked just fine with a quick sample text containing a bunch of line-breaks then using `replaceAll` to substitute them with
    elements & finally passing it to a `TextView` using `Html.fromHtml`. What ICS device are you using?
    – Jens Feb 07 '12 at 13:54
  • Kind of, and it's not. I've used the emulator and my Galaxy Nexus. Use the Last.fm api demo, you'll see what I mean. – adneal Feb 07 '12 at 21:46

1 Answers1

2

(removed old entry... tested the test project)

There is a extremely interesting "issue" but I am not sure who is responsible for it.

I tested your app and got it working. I was looking on a 2.2 device, where the first \n appears (index 413) in the string and than I took a look at this index at the ICS version. There I could only find a \r.

You can see the difference in the LogCat, too. On ICS you see scrambled text with no sense but below ICS you can read the complete text without any problem.

So instead of replacing \n with <br/> you should replace \r, too. I would, just to make sure, replace in this order:

  1. \n\r
  2. \n
  3. \r

Step 1 is important to remove the possibility, that you "double" the breaks if someone uses the \n\r...

Used working code:

String artistText = artistInfo.getWikiText();
Log.i("Log", artistText);
artistText = artistText.replaceAll("\n\r", "<br />");
artistText = artistText.replaceAll("\n", "<br />");
artistText = artistText.replaceAll("\r", "<br />");
System.out.println(artistText);
txtWiki.setText(Html.fromHtml(artistText));
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • I can't copy and past the text I'm using, because I download it and it's different for each artist. So, I can't manually place the break tags inside of it. I've tried using "\n\r" and replacing the breaks before and gave it another shot just now, but nothing changes. So, I'm still in the dark. – adneal Feb 07 '12 at 21:48
  • Could you add a sample text you got from the API? – WarrenFaith Feb 07 '12 at 21:50
  • You can take a look at the actual text being returned [from the URL.](http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Bon+Iver&api_key=b25b959554ed76058ac220b7b2e0a026) It's at the very bottom. It starts at "content". – adneal Feb 07 '12 at 21:57
  • So, you tested `replaceAll("\n\r\n\r", "
    ")` on Ice Cream Sandwich and it worked?
    – adneal Feb 08 '12 at 11:52
  • No, I used `replaceAll("\n", "
    ")` and on the next line `replaceAll("\r", "
    ")` and it worked. I would use `replaceAll("\n\r", "
    ")` at the very first, too.
    – WarrenFaith Feb 08 '12 at 11:55
  • Ah ha! Wow, I was so close the entire time. Anyway, that's awesome you got it working. Thanks. It certainly is interesting. – adneal Feb 08 '12 at 12:00