Skip to content

Android strings.xml character escaping

Escaping Characters in Android’s strings.xml

Occasionally, we want to include strings with apostrophes ' or double-quotes " in our Android strings.xml file. However, if these characters are not properly escaped, it can lead to compilation errors.

For example, the following strings would cause a compilation error:

strings.xml
<string name="guide1">Check your phone's battery</string>
<string name="guide2">He said "let's go"</string>

To resolve this issue, you must escape these characters:

  • Apostrophe ' should be escaped by using \'.
  • Double-quotes " should be escaped by using \".

The corrected strings.xml should look like this:

strings.xml
<string name="guide1">Check your phone\'s battery</string>
<string name="guide2">He said \"let\'s go\"</string>

For more details, you can refer to the official Android documentation on string resource formatting and escaping.

Summary

When working with Android’s strings.xml, it’s crucial to properly escape special characters like apostrophes and double-quotes to avoid compilation errors. By using \' for apostrophes and \" for double-quotes, you can ensure that your strings are correctly parsed by the Android resource compiler. Always refer to the official documentation for additional formatting and styling guidelines.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!