24

I am using webservice in my android application. In my webservice result I get a HTML formatted String as result.

Assume this is my web service result:

Books on Chennai:\n

  • Madras Discovered, Tales of Old and New Madras, Madras (1992) by S. Muthiah
  • \n
  • Madras – its Past and its Present (1995) by S. Muthiah
  • \n
  • Madras – its Yesterdays and Todays and Tomorrows by S. Muthiah
  • \n
  • At Home in Madras (1989) by S. Muthiah
  • \n
  • The Spirit of Chepauk (1998) by S. Muthiah
  • \n
  • The Story Of Fort St. George (1945) by Col. D.M. Reid
  • \nFiction set in Chennai\n
  • Kalyani’s Husband by S. Y. Krishnaswamy
  • \n
  • Chasing Rainbows in Chennai (http://chasingrainbowsinchennai.blogspot.com/\">Chasing Rainbows in Chennai), (2003) by Colin Todhunter
  • \n
  • In Old Madras (1914) by Bithia Mary Crocker
  • \n

I am using Html.fromHtml(String) to add this to my TextView. But I am not getting new line feature. I displays it as a paragraph.

Books on Chennai: Madras Discovered, Tales of Old and New Madras, Madras (1992) by S. Muthiah Madras – its Past and its Present (1995) by S. Muthiah Madras – its Yesterdays and Todays and Tomorrows by S. Muthiah At Home in Madras (1989) by S. Muthiah The Spirit of Chepauk (1998) by S. Muthiah The Story Of Fort St. George (1945) by Col. D.M. ReidFiction set in Chennai Kalyani’s Husband by S.Y. Krishnaswamy Chasing Rainbows in Chennai (Chasing Rainbows in Chennai), (2003) by Colin Todhunter In Old Madras (1914) by Bithia Mary Crocker

How can I present this data in an easily readable and understandable way?

ydnas
  • 519
  • 1
  • 12
  • 29

7 Answers7

90

Having this string as an example:

String sBooks = "Books on Chennai:\n ....";
    

Replace: \n with <br>:

sBooks = sBooks.replace("\n", "<br>");

Example using a TextView:

myTextView.setText(Html.fromHtml(sBooks));
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • 4
    Funny, `
    ` was the first thing I tried, and it didn't work. I guess fromHTML really doesn't like self-closing tags.
    – BlueMonkMN Feb 10 '18 at 21:07
  • 3
    Another way is to use official documentation and put **<**. E.g.: `Email is: <b>%1$s</b><br/>Nickname is: <b>%2$s</b>` More info at: [Android String Resources](https://developer.android.com/guide/topics/resources/string-resource.html) – Oleg Novosad Mar 25 '19 at 13:08
  • Everyone suggest to use
    It is not working. But working with
    – Dipin P J Nov 03 '21 at 19:23
6

Just replace \n with <br/> tag and then pass the whole string to Html.fromHtml(String). By this it will be displayed in proper format.

The Heist
  • 1,444
  • 1
  • 16
  • 32
  • Let, you are getting string as `String str`, then: String newLine = "
    "; `Html.fromHtml(str.replace("\n", newLine));`
    – The Heist Feb 10 '14 at 06:17
3

You should replace this \n with <br>. For more android HTML tags support go to Android Support HTML Tags or daniel-codes.blogspot.in

M D
  • 47,665
  • 9
  • 93
  • 114
1

Use this:

b.setText(Html.fromHtml("<b>" + your string+ "</b>" + "<br/>" + cursor.getString(1)));
Lokesh
  • 5,180
  • 4
  • 27
  • 42
  • Coy and Paste [http://stackoverflow.com/questions/6342042/how-do-i-add-a-new-line-in-html-format-in-android](http://stackoverflow.com/questions/6342042/how-do-i-add-a-new-line-in-html-format-in-android) – M D Feb 10 '14 at 06:14
1

If you are following kotlin

const val HTML_TAGS = " <br/> "
const val NEW_LINE = "\n"
      
val description = targetText?.replace(NEW_LINE, HTML_TAGS) ?: ""
text_view.text = HtmlCompat.fromHtml(description, HtmlCompat.FROM_HTML_MODE_LEGACY)
Ebin Joy
  • 2,690
  • 5
  • 26
  • 39
0

The best option is instead of TextView use a WebView and

final String mimeType = "text/html";
    final String encoding = "UTF-8";
    webView.loadDataWithBaseURL("", contentToShowString, mimeType,
            encoding, "");
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
Ameer
  • 2,709
  • 1
  • 28
  • 44
0

have you tried with \r\n ?`

If this not works then you can try with : \n with escape sequence : \\\n
I hope this will help you :)

Something like this

string = string.replace("\\\n", System.getProperty("line.separator"));
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
Singh
  • 554
  • 6
  • 22