501

I wrote the following in the strings.xml file:

<string name="game_settings_dragNDropMove_checkBox">Move by Drag&Drop</string>

I got the following error:

The reference to entity "Drop" must end with the ';' delimiter.

How can I write character & in the strings.xml?

Samet ÖZTOPRAK
  • 3,112
  • 3
  • 32
  • 33
Palo
  • 10,591
  • 7
  • 28
  • 34

14 Answers14

1174

Encode it:

&amp;
Bob Fincheimer
  • 17,978
  • 1
  • 29
  • 54
  • 5
    If using CDATA then be aware that some characters are reserved. © -> © Refer to this article. http://www.w3schools.com/html/html_entities.asp – toidiu Dec 28 '15 at 19:20
  • 3
    \u0026 Better to use unicode as suggested by @Moss – Neo Apr 10 '16 at 16:01
170

For special character I normally use the Unicode definition, for the '&' for example: \u0026 if I am correct. Here is a nice reference page: http://jrgraphix.net/research/unicode_blocks.php?block=0

Moss
  • 6,002
  • 1
  • 35
  • 40
81

This is a my issues, my solution is as following: Use &gt; for >, &lt;for < , &amp; for & ,"'" for ' , &quot for \"\"

Hai Rom
  • 1,751
  • 16
  • 9
  • 2
    Cannot edit this because the change is tiny, leaving comment instead. It should read `Use > for >, <for <` – Jose_GD Jun 04 '18 at 20:28
27

Even your question is answered, still i want tell more entities same like this. These are html entities, so in android you will write them like:

Replace below with:

& with &amp;
> with &gt;
< with &lt;
" with &quot;, &ldquo; or &rdquo;
' with &apos;, &lsquo; or &rsquo;
} with &#125;
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
10

It should be like this :

<string name="game_settings_dragNDropMove_checkBox">Move by Drag&amp;Drop</string>
Faruk
  • 5,438
  • 3
  • 30
  • 46
7

& in string like:

<string name="string_abc">Translate &amp; Scan Objects</string>

Output will be:

Translate & Scan Objects

Ghayas
  • 1,266
  • 10
  • 17
6

It is also possible put the contents of your string into a XML CDATA, like Android Studio does for you when you Extract string resource

<string name="game_settings_dragNDropMove_checkBox"><![CDATA[Move by Drag&Drop]]></string>

caulitomaz
  • 2,141
  • 14
  • 20
4

This may be very old. But for those whose looking for a quick code.

 public String handleEscapeCharacter( String str ) {
    String[] escapeCharacters = { "&gt;", "&lt;", "&amp;", "&quot;", "&apos;" };
    String[] onReadableCharacter = {">", "<", "&", "\"\"", "'"};
    for (int i = 0; i < escapeCharacters.length; i++) {
        str = str.replace(escapeCharacters[i], onReadableCharacter[i]);
    } return str;
 }

That handles escape characters, you can add characters and symbols on their respective arrays.

-Cheers

ralphgabb
  • 10,298
  • 3
  • 47
  • 56
4

To avoid the error, use extract string:

<string name="travels_tours_pvt_ltd"><![CDATA[Travels & Tours (Pvt) Ltd.]]></string>
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
Thamays
  • 2,978
  • 26
  • 31
1

You can write in this way

<string name="you_me">You &#38; Me<string>

Output: You & Me

Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
Hoque MD Zahidul
  • 10,560
  • 2
  • 37
  • 40
1

Mac and Android studio users:

Type your char such as & in the string.xml or layout and choose "Option" and "return" keys. Please refer the screen shot

MetaPlanet
  • 129
  • 4
1

If you want to hardcode the string in xml directly, you can add it up this way.

  <TextView 
     android:layout_marginStart="5dp"
     android:layout_marginLeft="5dp"
     android:layout_centerVertical="true"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="You &amp; Me"
Mubarak Tahir
  • 207
  • 3
  • 4
0

The simplest way I've found is to replace the '&' with the Unicode equivalent. So for the example given write:

<string name="game_settings_dragNDropMove_checkBox">Move by Drag\u0026Drop</string>
0

I'm posting this because none of the above encodings worked for me.

I found that replacing the ampersand with the sequence: &#38; worked as an xml string resource. As in:

<string name="webLink"><a href="https://www.example.com?param1=option1&#38;param2=option2">click here</a></string>