79

when editing a String in XML I need to add line breaks. And I wanted to ask what is the RIGHT form when programming for android? Because <br> works but ECLIPSE marks the area as problematic. If I check out suggestions Eclipse tells me that I shall add a end tag </br> - IF I add that the line break dissapears...

So the one works but is marked as problematic, the other works not but Eclipse tells me its ok..

What form shall I use?

Oliver Goossens
  • 1,903
  • 3
  • 20
  • 26
  • 4
    On a side-note: **Never** use `
    `. Explaination: most markup-languages (for xml, html, etc) use `` to open a tag, `` to close a tag and `` is an empty thus has an alternative notation ``. So `
    ` opens a tag, it's logic the IDE asksed to close with `` but as the br-tag is almost always empty it's advisable to use **`
    `**. Anyhow the question was about Java, so `\n` is much better. Using html for a textview works fine but consumes relatively much memory. Only use this it when necessary, if you want a multicolored textview or something like that.
    – T_D May 31 '13 at 08:00
  • See my answer here: http://stackoverflow.com/questions/18938403/how-to-preserve-line-breaks-in-xml-string-resources-in-android/31683548#31683548 – Jin Jul 28 '15 at 17:46

5 Answers5

150

Use \n for a line break and \t if you want to insert a tab.

You can also use some XML tags for basic formatting: <b> for bold text, <i> for italics, and <u> for underlined text.

Other formatting options are shown in this article on the Android Developers' site:
https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

Ordago
  • 3
  • 3
Christopher Orr
  • 110,418
  • 27
  • 198
  • 193
17

If you are refering to res strings, use CDATA with \n.

<string name="about">
    <![CDATA[
 Author: Sergio Abreu\n
 http://sites.sitesbr.net
  ]]>        
</string>
Sergio Abreu
  • 2,686
  • 25
  • 20
4

Take note: I have seen other posts that say &#xA; will give you a paragraph break, which oddly enough works in the Android xml String.xml file, but will NOT show up in a device when testing (no breaks at all show up). Therefore, the \n shows up on both.

Azurespot
  • 3,066
  • 3
  • 45
  • 73
4

Also you can add <br> instead of \n.

And then you can add text to TexView:

articleTextView.setText(Html.fromHtml(textForTextView));
mobiledev Alex
  • 2,228
  • 2
  • 28
  • 30
2

Here is what I use when I don't have access to the source string, e.g. for downloaded HTML:

// replace newlines with <br>
public static String replaceNewlinesWithBreaks(String source) {
    return source != null ? source.replaceAll("(?:\n|\r\n)","<br>") : "";
}

For XML you should probably edit that to replace with <br/> instead.

Example of its use in a function (additional calls removed for clarity):

// remove HTML tags but preserve supported HTML text styling (if there is any)
public static CharSequence getStyledTextFromHtml(String source) {
    return android.text.Html.fromHtml(replaceNewlinesWithBreaks(source));
}

...and a further example:

textView.setText(getStyledTextFromHtml(someString));
Lorne Laliberte
  • 6,261
  • 2
  • 35
  • 36