16

I'm trying to use multi-line constants (defined in .xml file under /res/values/ folder), but it seems it's impossible to preserve line breaks there - they all being converted in spaces. I've tried to play with "formatted" attribute of strings (setting it both to "true" and "false", also I've tried wrapping strings in CDATA tags, like this:

<string name="str1">
A
B
C
</string>

<string name="str2" formatted="true">
A
B
C
</string>

<string name="str3" formatted="false">
A
B
C
</string>

<string name="str4"><![CDATA[
A
B
C
]]></string>

<string name="str5" formatted="true"><![CDATA[
A
B
C
]]></string>

<string name="str6" formatted="false"><![CDATA[
A
B
C
]]></string>

All these string declaration variants produce identical results - five-character string "A B C" (line breaks replaced by single space). Is there any way to avoid this?

P.S. I understand that I can use "\n" to insert line breaks, but anyway resulting string will contain spaces in place of actual line-breaks; i.e., following declaration:

<string name="str1">
A\n
B\n
C\n
</string>

results in string "A\n B\n C\n" (every manually inserted line break followed by annoying space). Is there any workaround?..

Denys Avilov
  • 427
  • 1
  • 4
  • 8
  • See http://stackoverflow.com/questions/10917555/adding-a-new-line-break-tag-in-xml - the answer is to use an HTML entity for the LF. – Mike C Jul 03 '15 at 00:29
  • *(…) it's impossible to preserve line breaks there - they all being converted in spaces* – This is not true. In all cases the first (before *A*) and the last (after *C*) line break were clearly removed. – Piotr Dobrogost Jun 06 '16 at 12:37

7 Answers7

27

In case people are having problems with this, inserting \n at the end of XML string causes indentation. For example:

<string name="test">
    A\n
    B\n
    C
</string>

Will appear as

A
  B
  C

Instead, you need to insert \n at BEGINNING of XML strings

<string name="test">
    A
    \nB
    \nC
</string>

This will cause the new lines to show up properly.

Jin
  • 6,055
  • 2
  • 39
  • 72
13

Use another backslash at the end of each line:

<string name="str1">
A\n\
B\n\
C\n\
</string>

This way, you insert line breaks using the "\n" and you escape the line end in the resource file so it is not converted to space.

Still ugly, but it works.

Moritz Both
  • 1,598
  • 1
  • 13
  • 21
  • Jin's answer is good, but this answer is better and should be the accepted answer. – swooby Jul 08 '16 at 04:51
  • I have tested this on API 17 with Android Studio 2.1 and it doesn't work. – cpx Jul 09 '16 at 07:38
  • Still on API 25 and Android Studio 3.4 doesn't work. `\nA` "unfortunately" is still a better/working solution. – Jack T Nov 27 '19 at 09:47
4

Use html breaks like this:

<string name="your_string">
<![CDATA[
<p>
    Here is your text.<br/>
    This text is on a new line.<br/>
    So is this text. \n
    This is on the previous line with a space added.
</p>
]]>
</string>
3

To add a newline from XML, you need to add "\n".

This is how you do it.

<string name="testing">A\nB\nC</string>

<TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:lines="4"
        android:text="@string/testing"/>

Hope this helps

ShujatAli
  • 1,376
  • 2
  • 14
  • 26
  • 2
    Result is just single-lined string "A B C" – Denys Avilov Sep 22 '13 at 16:13
  • **@shujatAli**, have you read postscriptum to my question? Sure I know I can use "\n", but question is to use large string constants spanning multiple lines and to preserve line breaks in places where they are in source xml-file. For example, I'm trying to store formatted SQL-queries in constants in xml-file, because most of them are pretty large (often dozens of lines) and it is inconvenient to define such a strings directly in source code. "Formatted" here means just "with line-breaks". To achieve this, currently I have to add "\n" to end of every line, which is somewhat annoying. – Denys Avilov Sep 23 '13 at 15:11
  • Moreover, aapt inserts extra space in place of every real line break in source xml. Unfortunately, it seems that there is no any better solution. – Denys Avilov Sep 23 '13 at 15:12
1

I finally found a way that does not add extra spaces anywhere and looks okay for me:

<string name="test">
A\
B\
C\
</string>

And another one:

<string name="test">
"A
B
C"
</string>

Tested with Android Studio 4.1.2 on API 21 & 30.

gmk57
  • 839
  • 11
  • 20
0

My two cents.
I solved using string arrays, loading the resource and then concatenating the elements of the array.
I also find it nice to have each long string in a separate file.

String[] storages = getResources().getStringArray(R.array.storages);
String oneline = new String();
    for (int i = 0; i < storages.length; i++) {
        oneline = oneline.concat(storages[i]);
        if (i < storages.length - 1)
            oneline.concat(System.getProperty("line.separator"));
    }
kekolab
  • 801
  • 10
  • 24
0

In latest android version you have only one solution.

Your string data must look like this.

<string name="thanks_from_head_shield">
    Mr. Prasanna Sumudu Chandrasekara<br />
    https://www.java2s.com<br />
    https://www.oracle.com/java<br />
    https://www.javatongue.blogspot.com<br />
    https://www.coderwall.com<br />
    https://www.igetintopc.com<br />
    https://www.tutorialspoint.com<br />
    https://stackoverflow.com<br />
    https://www.javatpoint.com<br />
    https://coderanch.com<br />
    https://www.c-sharpcorner.com<br />
    https://javapointers.com<br />
    https://www.fatalerrors.org<br />
    https://dzone.com<br />
    https://www.javamadesoeasy.com<br />
    https://www.geeksforgeeks.org<br />
    https://www.jetbrains.com<br />
    https://www.developer.android.com<br />
    Adobe.Inc<br />
    Thanks for other all softwares I used  and Other<br />
</string>

You can add <br /> tag without <![CDATA[...]]> tag. This is 100% working in Android studio 4.2.1. But I only tested it with API Level 30. @Chris Moore is incorrect for android studio 4.2.1.

DSF.Inc
  • 3,021
  • 2
  • 6
  • 9