How to keep the spaces at the end and/or at the beginning of a String?

This documentation suggests quoting will work:

<string name="my_str_spaces">" Before and after? "</string>

I ran into the same issue. I wanted to leave a blank at the end of a resource string representing an on-screen field name.

I found a solution on this issue report : https://github.com/iBotPeaches/Apktool/issues/124

This is the same idea that Duessi suggests. Insert \u0020 directly in the XML for a blank you would like to preserve.

Example :

<string name="your_id">Score :\u0020</string>

The replacement is done at build time, therefore it will not affect the performance of your game.


I just use the UTF code for space "\u0020" in the strings.xml file.

<string name="some_string">\u0020The name of my string.\u0020\u0020</string>

works great. (Android loves UTF codes)


Even if you use string formatting, sometimes you still need white spaces at the beginning or the end of your string. For these cases, neither escaping with \, nor xml:space attribute helps. You must use HTML entity &#160; for a whitespace.

Use &#160; for non-breakable whitespace.
Use &#032; for regular space.