Home » Android » How to add < and > in android strings.xml

How to add < and > in android strings.xml

Posted by: admin May 14, 2020 Leave a comment

Questions:

I need to add <string name="name_hint"><Given Name></string> in strings.xml. What is the escape sequence in order to do that? The string to be added is <Given Name>

How to&Answers:

Use &lt; for less than symbol and &gt; for greater than symbol.
so here you need to replace like:

<string name="name_hint">&lt;Given Name&gt;</string>

Answer:

The Android string.xml file is just a regular XML file, so you can use the XML escape characters:

"   &quot;
'   &apos;
<   &lt;
>   &gt;
&   &amp;

Your example would become <string name="name_hint">&lt;Given Name&gt;</string>

UPDATE:
Furthermore you can use the HTML entities of each character, a blank space is for example , and a non-breaking space is  .

Note that I am not entirely sure whether this is officially supported by the XML standards, or that it ‘just works’…

Answer:

You can use “<” for “<” and “>” for “>”
So you should add the following string in your app’s strings.xml like:

<string name="name_hint">&lt;Given Name&gt;</string>

For your another requirement you can define it like

<Given name> <Middlename> <Sirname>

<string name="name_hint">&lt;Given Name&gt; &lt;Middle Name&gt; &lt;Last Name&gt; </string>

or define three different strings.

<string name="name_hint1">&lt;Given Name&gt;</string>
<string name="name_hint2">&lt;Middle Name&gt;</string>
<string name="name_hint3">&lt;Last Name&gt;</string>

in your java file add it dynamically

tv.setText(getResources().getString(R.string.name_hint1) + "  " +
  getResources().getString(R.string.name_hint2)+"  " + 
  getResources().getString(R.string.name_hint3));