Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line Break in XML formatting?

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?

like image 763
Oliver Goossens Avatar asked May 20 '10 21:05

Oliver Goossens


People also ask

Are line breaks allowed in XML?

XML does not require a specific form of line break, so you can use whatever is convenient (carriage return, linefeed, or a combination) when creating an XML file. XML parsers will do the right thing largely because they're parsing on tags, not records - so whatever line break you're using is just whitespace to XML.

Does new line matter in XML?

XML doesn't require "lines", it's a nested storage structure and any additional newlines and whitespace will be ignored, and they just take up additional storage space.

Does BR work in XML?

<br /> Doesn't work in XML.

How do I comment out a single line in XML?

The syntax for adding XML comments in your code is triple slashes /// followed by one of the supported XML tags.


5 Answers

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

like image 172
Christopher Orr Avatar answered Nov 12 '22 22:11

Christopher Orr


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

<string name="about">
    <![CDATA[
 Author: Sergio Abreu\n
 http://sites.sitesbr.net
  ]]>        
</string>
like image 32
Sergio Abreu Avatar answered Nov 12 '22 23:11

Sergio Abreu


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

And then you can add text to TexView:

articleTextView.setText(Html.fromHtml(textForTextView));
like image 4
mobiledev Alex Avatar answered Nov 13 '22 00:11

mobiledev Alex


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.

like image 4
Azurespot Avatar answered Nov 13 '22 00:11

Azurespot


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));
like image 2
Lorne Laliberte Avatar answered Nov 12 '22 22:11

Lorne Laliberte