String Resource new line /n not possible?
Solution 1
use a blackslash not a forwardslash. \n
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Hello\nWorld!</string>
</resources>
Also, if you plan on using the string as HTML, you can use <br />
for a line break(<br />
)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Hello<br />World!</string>
</resources>
Solution 2
I know this is pretty old question but it topped the list when I searched. So I wanted to update with another method.
In the strings.xml file you can do the \n or you can simply press enter:
<string name="Your string name" > This is your string. This is the second line of your string.\n\n Third line of your string.</string>
This will result in the following on your TextView:
This is your string.
This is the second line of your string.
Third line of your string.
This is because there were two returns between the beginning declaration of the string and the new line. I also added the \n to it for clarity, as either can be used. I like to use the carriage returns in the xml to be able to see a list or whatever multiline string I have. My two cents.
Solution 3
After I tried next solution
- add
\n
- add
\n
without spaces after it - use
""
and press Enter inside text - text with press Enter
- use
lines=2
What solves me is br
tag
<string name="successfullyfeatured">successfully<br/> You are now a member</string>
Update
the most reliable solution is to use Translation editor and edit text and it will handle new lines for you
Solution 4
This is an old question, but I found that when you create a string like this:
<string name="newline_test">My
New line test</string>
The output in your app will be like this (no newline)
My New line test
When you put the string in quotation marks
<string name="newline_test">"My
New line test"</string>
the newline will appear:
My
New line test
Solution 5
When using the translations editor in Android Studio just click the icon to the right (or use Shift-Enter on Windows, Alt/Option-Enter on MacOS) and then add line breaks using return.
This will insert \n
correctly in the localized strings.xml.
Related videos on Youtube
Andi Jay
Updated on December 07, 2021Comments
-
Andi Jay over 2 years
It doesn't seem like it's possible to add a new line
/n
to an XML resource string. Is there another way of doing this?-
Keith about 13 yearsyou mean \n? do you have an example?
-
akash89 almost 9 yearsIt is supposed to be \n. Works for me
-
Vega almost 4 years@anandtripathi, I rolled back your edit because it made the answers meaningless. The OP had wrongly use \n instead of /n, which is the essence of the question. We should not correct OP's code, but only the question body, for the obvious raisons
-
Kuvonchbek Yakubov about 3 yearsRebuilding project may help.
-
-
Andi Jay over 10 yearsOh, that's interesting. thanks for sharing. Also, I feel like an idiot for asking this question, but it looks like it has helped a lot of people. LOL!!
-
akash89 over 8 yearsIn strings.xml only HTML format it accepts, so <br /> is the appropriate solution provided
-
Daniel Argüelles almost 8 yearsOnly happens when you add the string with the wizard. If you edit the xml it works
-
Kanat over 7 yearsI made this mistake too. Thanks for your advice. But I just corrected all appearances of \n myself.
-
Pierre-Luc Paour about 7 yearsTwo successive linebreaks are treated by (at least current versions of) Android as just a single space. github.com/paour/StringResourceTest
-
dephinera almost 7 yearsNot a good solution. You should get the text from the resources and use it directly. What if you have to handle more special characters? You gonna write more code for them too? What if you have edge cases?
-
Ahmad Reza Enshaee about 6 yearsWorks for me but just in case, remember when you're defining resources like above, you should avoid using
Html.fromHtml
. Thanks -
Jans Rautenbach over 5 yearsI want to mention something for people like me reaching this page because of Android Studio. In AS when you extract a string using the IDE's tool, it automatically strips newline characters. You can just manually add them back in and it'll work fine. Not sure why it does this.
-
Simple over 5 yearsThanks a lot for this answer.
-
Thelouras over 5 years\n doest work for me , neither change the line neither string has /n . Do you know why ? neither hit enter works , that said in other answers
-
Tibor Nagy over 5 yearsThere is no icon to the right and Shift-Enter jumps to the next text above, at least on ubuntu.
-
Nolonar almost 4 yearsWrapping the multiline string in quote marks worked perfectly. Much better than putting
\n
everywhere (which can quickly become unreadable and confusing). Needless to say, but just in case: if you want to have a literal quote mark in your text, you must escape it:\"
-
jvargas almost 4 yearsHow can I make two break lines?
-
jvargas almost 4 yearsHow can I make two break lines?
-
Metalfreak over 3 yearsIt seems that at least in the Translations Editor you need to use forward slash, otherwise it it will simply interpret it as a literal \n. Talk about consistent design....
-
The Fluffy T Rex over 3 yearsUpdated this with info on where to find the button.
-
Simmam over 3 yearsIn Android Studio Translation editor, one has to select the text item, then come down to the bottom of the Translation editor window, where Key, Default value, Translation are displayed, then click on the value (used to edit the text / content), press the "Shift + Enter" here, this opens a text editing window. Here you can create paragraphs as it has to appear and save it. This will be used and displayed as such as paragraphs.
-
apmartin1991 about 3 years@jvargas \n\n for two. \n\n\n for three. and so on...
-
Fatih about 3 yearsJust behaves differently on different platforms for me.
-
BryanT almost 3 yearsMy layout preview really did display 'SPEED\nDEPTH" on a button until after a complete rebuild. After that my buttons had the correct two line caption.
-
schnondle almost 3 yearsjust a more recent update on this: neither
\n
or/n
work in strings.xml files. neither does<br />
. however just using<br />
works perfectly for this! I tested them just to make sure, and<br />
is the only one that worked as expected! -
Marcel Hofgesang over 2 yearsPlease share some code sample of how you tried and how you solved it.
-
ilidiocn over 2 yearsIn fact the correct way to use it is \n although it is possible to see it done after Build the application. The <br/> only has an effect on the design and not on the application <string name="available_balance">Available\nBalance</string>
-
Jeremy Caney over 2 yearsYou say you "finally" found the solution, but a solution was provided over a decade ago, and has been widely validated by the community. What does your solution add that isn't already addressed in the accepted answer—or the other seventeen answers, for that matter?
-
MehranB over 2 years
\n
works at run time but not in the layout inspector.