How to add < and > in android strings.xml

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 &#032;, and a non-breaking space is &#160;.

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


You can use &lt; for < and &gt; 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));

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>

Tags:

Android