Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit multiline strings in Android strings.xml file?

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?

like image 851
user387184 Avatar asked Oct 22 '11 15:10

user387184


1 Answers

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(

like image 61
flying sheep Avatar answered Sep 18 '22 19:09

flying sheep