String Resource new line /n not possible?

203,385

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 &lt;br /&gt; for a line break(<br />)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="title">Hello&lt;br /&gt;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.

Share:
203,385

Related videos on Youtube

Andi Jay
Author by

Andi Jay

Updated on December 07, 2021

Comments

  • Andi Jay
    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
      Keith about 13 years
      you mean \n? do you have an example?
    • akash89
      akash89 almost 9 years
      It is supposed to be \n. Works for me
    • Vega
      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
      Kuvonchbek Yakubov about 3 years
      Rebuilding project may help.
  • Andi Jay
    Andi Jay over 10 years
    Oh, 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
    akash89 over 8 years
    In strings.xml only HTML format it accepts, so &lt;br /&gt; is the appropriate solution provided
  • Daniel Argüelles
    Daniel Argüelles almost 8 years
    Only happens when you add the string with the wizard. If you edit the xml it works
  • Kanat
    Kanat over 7 years
    I made this mistake too. Thanks for your advice. But I just corrected all appearances of \n myself.
  • Pierre-Luc Paour
    Pierre-Luc Paour about 7 years
    Two successive linebreaks are treated by (at least current versions of) Android as just a single space. github.com/paour/StringResourceTest
  • dephinera
    dephinera almost 7 years
    Not 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
    Ahmad Reza Enshaee about 6 years
    Works for me but just in case, remember when you're defining resources like above, you should avoid using Html.fromHtml. Thanks
  • Jans Rautenbach
    Jans Rautenbach over 5 years
    I 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
    Simple over 5 years
    Thanks a lot for this answer.
  • Thelouras
    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
    Tibor Nagy over 5 years
    There is no icon to the right and Shift-Enter jumps to the next text above, at least on ubuntu.
  • Nolonar
    Nolonar almost 4 years
    Wrapping 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
    jvargas almost 4 years
    How can I make two break lines?
  • jvargas
    jvargas almost 4 years
    How can I make two break lines?
  • Metalfreak
    Metalfreak over 3 years
    It 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
    The Fluffy T Rex over 3 years
    Updated this with info on where to find the button.
  • Simmam
    Simmam over 3 years
    In 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
    apmartin1991 about 3 years
    @jvargas \n\n for two. \n\n\n for three. and so on...
  • Fatih
    Fatih about 3 years
    Just behaves differently on different platforms for me.
  • BryanT
    BryanT almost 3 years
    My 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
    schnondle almost 3 years
    just a more recent update on this: neither \n or /n work in strings.xml files. neither does &lt;br /&gt;. 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
    Marcel Hofgesang over 2 years
    Please share some code sample of how you tried and how you solved it.
  • ilidiocn
    ilidiocn over 2 years
    In 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
    Jeremy Caney over 2 years
    You 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
    MehranB over 2 years
    \n works at run time but not in the layout inspector.