222

i have defined some strings in the strings.xml file. Now I need to put some extra space between some numbers in the string. When I type extra space characters this is not showing on the application though.

Before:

<string name="spelatonertext3">-4, 5, -5, 6, -6,

And if I put extra space like this:

<string name="spelatonertext3">-4,  5, -5,   6,  -6,

It just looks the same on the app. How can I make space characters into the XML string?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Andreas
  • 2,243
  • 2
  • 14
  • 7
  • Well, it looked the same in this page also...:) thing is i want some space between the numbers, how is that possible..? – Andreas Jun 02 '12 at 14:14
  • Use code formatting to preserve the spaces (& `<>` content) on SO. :) – Andrew Thompson Jun 02 '12 at 14:15
  • Possible duplicate of [Android - String concatenate - how to keep the spaces at the end and/or beginning of String?](http://stackoverflow.com/questions/1587056/android-string-concatenate-how-to-keep-the-spaces-at-the-end-and-or-beginnin) – Sruit A.Suk Mar 03 '16 at 10:10

16 Answers16

495

to use white space in xml as string use &#160;. XML won't take white space as it is. it will trim the white space before setting it. So use &#160; instead of single white space

Community
  • 1
  • 1
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • 1
    This doesnt work with wrap_content of a textview, when the space occurs at the end of the text. The textview does not maintain the width that is taken up by the 'space' character. – Etienne Lawlor Nov 23 '13 at 04:13
  • 37
    This will add a non-breakable space. It is different from a regular space and will not work well with certain functions (XPath's `normalize-space`, for example). Try using ` ` (regular space) instead of ` `. @toobsco42 – shwartz Feb 10 '14 at 16:43
  • I had to use ` ` instead of `\u0020` to place a non-breaking space after the area code in a phone number. Otherwise, the phone number was being wrapped at the end of the line after the area code's trailing parenthesis. – mharper Apr 26 '17 at 22:09
  • 2
    is ok if you need 1 space. If you put several ones ( ...) they are trimmed to 1. I think that it is the same behaviour as a normal blank space so it seems useless. If found also suggestion about (supposed to be a tab character) : it displays nothing.   works well, it is what I need, thanks ρяσѕρєя K. – herve-guerin Oct 18 '17 at 17:01
77

Insert \u0020 directly in the XML for a blank you would like to preserve.

<string name="spelatonertext3">-4, \u00205, \u0020\u0020-5, \u00206, \u0020-6,</string>
K_Anas
  • 31,226
  • 9
  • 68
  • 81
44

Android doesn't support keeping the spaces at the end of the string in String.xml file so if you want space after string you need to use this unicode in between the words.

\u0020

It is a unicode space character.

saibaba vali
  • 2,641
  • 1
  • 17
  • 17
33

This should work as well. Use quotes to maintain space

<string name="spelatonertext3">"-4,  5, -5,   6,  -6,"</string>
pcodex
  • 1,812
  • 15
  • 16
  • 4
    This is the cleanest (easiest to read) answer in my opinion. Unfortunately most people probably don't make it this far down the answer list before moving on. – DavidR Jun 09 '20 at 22:55
  • 1
    This should be upvoted more. Very clean and readable, no unicode bs – Rafsanjani Jan 13 '21 at 12:25
14

As already mentioned the correct way to have a space in an XML file is by using \u0020 which is the unicode character for a space.

Example:

<string name="spelatonertext3">-4,\u00205,\u0020-5,\u00206,\u0020-6</string>

Other suggestions have said to use &#160; or &#032; but there is two downsides to this. The first downside is that these are ASCII characters so you are relying on something like a TextView to parse them. The second downside is that &#160; can sometimes cause strange wrapping in TextViews.

MShipman
  • 351
  • 4
  • 8
11

Put &#160; in string.xml file to indicate a single space in an android project.

maxxi
  • 197
  • 4
  • 11
9

Space variants:

<string name="space_demo">|&#x20;|&#x2009;|&#x200A;|</string>

| SPACE | THIN SPACE | HAIR SPACE |

Andrew
  • 36,676
  • 11
  • 141
  • 113
7

You can use following as well

<string name="spelatonertext3"> "-4,  5, -5,   6,  -6, "> </string>

Put anything in " "(quotation) with space, and it should work

Surjit Singh
  • 269
  • 3
  • 8
5

The only way I could get multiple spaces in middle of string.

<string name="some_string">Before" &#x20; &#x20; &#x20;"After</string>

Before   After
drod
  • 359
  • 5
  • 17
4

If you are trying to give Space Between 2 string OR In String file of android then do this in your string file . Implement this before your "String name" that you want to show. \u0020\u0020 For E.g.. \u0020\u0020\u0020\u0020\u0020\u0020Payment

Rakesh Jha
  • 279
  • 2
  • 7
4

As explained here https://www.programmersought.com/article/33576744031/ use  

Example

<string name="in">in &#160; </string>
ilidiocn
  • 323
  • 2
  • 5
3

As per your question if you want add spaces more than one in string resources their are many option to add spaces between character or word :

1.By default one space you can add directly in string resource file it working fine. but if give more than one space inside string resources file then it exclude that spaces. eg . -4, 5, -5, 6, -6,

  1. If you want add more extra spaces inside string resource file then uses:- i. adding unicode after character like

        <string name="test">-4,&#160;&#160;5,&#160;&#160;-5,&#160;&#160;6,&#160;&#160;-6,</string>
    

ii.you can use "\u0020"

<string name="test">-4,\u0020\u0020 5,\u0020\u00205 -5,\u0020\u00205 6,\u0020\u00205 -6,</string>
Sagar Chorage
  • 1,460
  • 12
  • 13
2

xml:space="preserve"

Works like a charm.

Edit: Wrong. Actually, it only works when the content is comprised of white spaces only.

Link

Community
  • 1
  • 1
PauLEffect
  • 423
  • 5
  • 17
1

If the output is HTML, then in HTML multiple spaces display as a single space. To prevent this, use non-breaking spaces (xA0) instead of ordinary spaces.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
1

In Android Studio, you can open the strings.xml file and then click on "Open editor" and in the editor you can type any thing and it will handle it for you to appear in the application exactly as you typed it.

I know It may be late to write this answer, but It may help someone.

Emad N.
  • 40
  • 5
0

You want to it display like "-4, 5, -5, 6, -6," (two spaces),you can add the following code to string.xml

<string name="spelatonertext3"> "-4,&#160;&#160;5,&#160;-5,&#160;&#160;6,&#160;&#160;-6,"</string>

  is display one space.

Hai Rom
  • 1,751
  • 16
  • 9