If you have a string in your strings.xml file that you want to have a space character in front or after the string itself, you may notice that this space gets trimmed away automatically.

To avoid the string getting trimmed away, you can use one of the following tricks:

Trick one – Wrapping the string in quotes

If you wrap the string in a pair of quotes, the space will not get stripped away.
Also, the quotes themselves does not show up in the interface, when the string is being used.

<string name="your_string">" This string gets a space before it."</string>

Trick two – Writing the space character in unicode

  • \u00A0 is unicode for space
<string name="your_string">\u00A0This string gets a space before it.</string>

(The HTML entity &#160 may also work as a space character)

Possibly a Trick number three – String formatting

It may be possible to do a string format, where you insert a space when doing the string formatting in the code. (I have not tested this yet)

Refer to the documentation on the bottom of this page: developer.android.com.

<string name="your_string">%1$s This string gets a space before it.</string>

And then formatting the string like this:

String text_with_space = String.format(getResources().getString(R.string.your_string), "");

Do you have any other useful tricks like this?