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 CORRECT form when programming for Android? Since <br> works, but ECLIPSE marks this area as problematic. If I check the sentences, Eclipse will tell me that I will add the end tag </br> - IF I add that the line break disappears ...

So, that one works, but is marked as problematic, the other one doesn't, but Eclipse tells me that everything is in order.

What form should I use?

+63
android xml break line
May 20 '10 at 9:03 p.m.
source share
5 answers

Use \n to break the line 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 developer site:
https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

+117
May 20, '10 at 21:11
source share

If you reference res lines, use CDATA with \ n.

 <string name="about"> <![CDATA[ Author: Sergio Abreu\n http://sites.sitesbr.net ]]> </string> 
+11
Dec 28 '12 at 12:48
source share

You can also add <br> instead of \ n.

And then you can add text to TexView:

 articleTextView.setText(Html.fromHtml(textForTextView)); 
+3
Jul 19 2018-11-11T00:
source share

Please note: I saw other posts that say &#xA; will provide you with a break paragraph, which, oddly enough, works in the Android xml String.xml file, but will not appear on the device during testing (without interruptions, everything appears). Therefore, \n maps to both.

+3
Mar 02 '15 at 5:46
source share

Here is what I use when I do not have access to the source string, for example. for loaded 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 change this instead of <br/> instead.

An 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 another example:

 textView.setText(getStyledTextFromHtml(someString)); 
+1
Apr 17 2018-12-12T00:
source share



All Articles