How to preserve line breaks in xml <string> resources in Android? How to preserve line breaks in xml <string> resources in Android? xml xml

How to preserve line breaks in xml <string> resources in Android?


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.


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.


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