16

I have several cases where my string in strings.xml is quite long and has multiple lines done with \n.

Editing however is quite annoying since it is a long line in Eclipse.

Is there a better way to edit this so it looks like it will be presented later in the textview, ie the line breaks as line breaks and the text in multiline edit mode?

Jonik
  • 80,077
  • 70
  • 264
  • 372
user387184
  • 10,953
  • 12
  • 77
  • 147
  • 2
    I don't think you can do that. But if you went down a line when each '\n' is used, it would look as it should. Plus, you can use 'control + i' in eclipse to organize the text. – Jong Oct 22 '11 at 15:48

4 Answers4

18

Two possibilities:

1. Use the Source, Luke

XML allows literal newline characters in strings:

<string name="breakfast">eggs
and
spam</string>

You just have to edit the XML code instead of using the nifty Eclipse GUI

2. Use actual text files

Everything inside the assets directory is available as a input stream from the application code.

You can access those file input streams of assets with AssetManager.open(), a AssetManager instance with Resources.getAssets(), and… you know what, here’s the Java-typical enormously verbose code for such a simple task:

View view;

//before calling the following, get your main
//View from somewhere and assign it to "view"

String getAsset(String fileName) throws IOException {
    AssetManager am = view.getContext().getResources().getAssets();
    InputStream is = am.open(fileName, AssetManager.ACCESS_BUFFER);
    return new Scanner(is).useDelimiter("\\Z").next();
}

the use of Scanner is obviously a shortcut m(

flying sheep
  • 8,475
  • 5
  • 56
  • 73
  • +1, especially for the assets tip. Works great for my email template. Btw, if using Guava, you could read an asset into string using `CharStreams.toString(new InputStreamReader(am.open(fileName), Charsets.UTF_8))`. – Jonik Dec 18 '13 at 09:11
  • The first one only works if you put the whole string in quotes – user3533716 Jul 03 '17 at 11:07
13

Sure, you could put newlines into the XML, but that won't give you line breaks. In strings.xml, as in all XML files, Newlines in string content are converted to spaces. Therefore, the declaration

<string name="breakfast">eggs
and
spam</string>

will be rendered as

eggs and spam

in a TextView. Fortunately, there's a simple way to have newlines in the source and in the output - use \n for your intended output newlines, and escape the real newlines in the source. The above declaration becomes

<string name="breakfast">eggs\n
and\n
spam</string>

which is rendered as

eggs
and
spam
Community
  • 1
  • 1
LightStruk
  • 1,451
  • 14
  • 17
  • Not sure what the extra slash at the end of the \n\ is? To get an extra white line use: \n\n – Meanman Nov 10 '14 at 12:37
  • New lines in the xml will give you line breaks if you put the whole string in quotes. However, then the xml indentation will give additional spaces before each row. – user3533716 Jul 03 '17 at 11:09
11

For anyone looking for a working solution that allows the XML String content to have multiple lines for maintainability and render those multiple lines in TextView outputs, simply put a \n at the beginning of the new line... not at the end of the previous line. As already mentioned, one or more new lines in the XML resource content is converted to one single empty space. Leading, trailing and multiple empty spaces are ignored. The idea is to put that empty space at the end of the previous line and put the \n at the beginning of the next line of content. Here is an XML String example:

<string name="myString">
    This is a sentence on line one.
    \nThis is a sentence on line two.
    \nThis is a partial sentence on line three of the XML
    that will be continued on line four of the XML but will be rendered completely on line three of the TextView.

    \n\nThis is a sentence on line five that skips an extra line.
</string>

This is rendered in the Text View as:

This is a sentence on line one.
This is a sentence on line two.
This is a partial sentence on line three of the XML that will be continued on line four of the XML but will be rendered completely on line three of the TextView.

This is a sentence on line five that skips an extra line.
Groove
  • 111
  • 1
  • 5
3

You may easily use "" and write any word even from other languages with out error:

<string name="Hello">"Hello world! سلام دنیا!" </string>

esy
  • 33
  • 3