How to write character & in android strings.xml

AndroidStringSpecial Characters

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

Encode it:

&amp;

Solution 2 - Android

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

Solution 3 - Android

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

Solution 4 - Android

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;

Solution 5 - Android

It should be like this :

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

Solution 6 - Android

You can find all the HTML Special Characters in this page http://www.degraeve.com/reference/specialcharacters.php Just replace the code where you want to put that character. :-)

Solution 7 - Android

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>

Solution 8 - Android

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

Solution 9 - Android

To avoid the error, use extract string:

<string name="travels_tours_pvt_ltd"><![CDATA[Travels & Tours (Pvt) Ltd.]]></string>

Solution 10 - Android

& in string like:

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

Output will be:

Translate & Scan Objects

Solution 11 - Android

You can write in this way

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

Output: You & Me

Solution 12 - Android

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

Solution 13 - Android

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"

Solution 14 - Android

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>

Solution 15 - Android

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>

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
QuestionPaloView Question on Stackoverflow
Solution 1 - AndroidBob FincheimerView Answer on Stackoverflow
Solution 2 - AndroidMossView Answer on Stackoverflow
Solution 3 - AndroidHai RomView Answer on Stackoverflow
Solution 4 - AndroidKhemraj SharmaView Answer on Stackoverflow
Solution 5 - AndroidfarukView Answer on Stackoverflow
Solution 6 - AndroidAZ_View Answer on Stackoverflow
Solution 7 - AndroidcaulitomazView Answer on Stackoverflow
Solution 8 - AndroidralphgabbView Answer on Stackoverflow
Solution 9 - AndroidA.G.THAMAYSView Answer on Stackoverflow
Solution 10 - AndroidGhayasView Answer on Stackoverflow
Solution 11 - AndroidHoque MD ZahidulView Answer on Stackoverflow
Solution 12 - AndroidMetaPlanetView Answer on Stackoverflow
Solution 13 - AndroidMubarak TahirView Answer on Stackoverflow
Solution 14 - Androiduser3194684View Answer on Stackoverflow
Solution 15 - Androiduser3194684View Answer on Stackoverflow