How to put space character into a string name in XML?

AndroidXmlWhitespace

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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>

Solution 3 - Android

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.

Solution 4 - Android

This should work as well. Use quotes to maintain space

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

Solution 5 - Android

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.

Solution 6 - Android

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

Solution 7 - Android

Space variants:

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

| SPACE | THIN SPACE | HAIR SPACE |

Solution 8 - Android

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

Solution 9 - Android

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

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

Before   After

Solution 10 - Android

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

Solution 11 - Android

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>

Solution 12 - Android

xml:space="preserve"

Works like a charm.

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

Link

Solution 13 - Android

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.

Solution 14 - Android

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.

Solution 15 - Android

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

Example

<string name="in">in &#160; </string

Solution 16 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndreasView Question on Stackoverflow
Solution 1 - Androidρяσѕρєя KView Answer on Stackoverflow
Solution 2 - AndroidK_AnasView Answer on Stackoverflow
Solution 3 - Androidsaibaba valiView Answer on Stackoverflow
Solution 4 - AndroidpcodexView Answer on Stackoverflow
Solution 5 - AndroidMShipmanView Answer on Stackoverflow
Solution 6 - AndroidmaxxiView Answer on Stackoverflow
Solution 7 - AndroidAndrewView Answer on Stackoverflow
Solution 8 - AndroidSurjit SinghView Answer on Stackoverflow
Solution 9 - AndroiddrodView Answer on Stackoverflow
Solution 10 - AndroidRakesh JhaView Answer on Stackoverflow
Solution 11 - AndroidSagar ChorageView Answer on Stackoverflow
Solution 12 - AndroidPauLEffectView Answer on Stackoverflow
Solution 13 - AndroidMichael KayView Answer on Stackoverflow
Solution 14 - AndroidEmad N.View Answer on Stackoverflow
Solution 15 - AndroidilidiocnView Answer on Stackoverflow
Solution 16 - AndroidHai RomView Answer on Stackoverflow