118

since the beginning of my programmation, I used some special character like "<-", ""<<" im my string.xml in Eclipse while developping for Android.

All worked fine for one year, but today, i just wanted to make some minor changes and began to edit my xml files.

I get now compilation error on these characters because eclipse believe it's part of the xml blocks.

Any idea on how I could add this symbol "<" in my xml files?

Thank a lot.

Waza_Be
  • 39,407
  • 49
  • 186
  • 260

3 Answers3

306

Use

&lt; for <

&gt; for >

&amp; for &

Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57
reece
  • 7,945
  • 1
  • 26
  • 28
27

Another way to insert special character follow Moss guide: How can I write character & in android strings.xml by used Unicode definition:

Example:

<string name="item_unknown">\u003c Item Unknown \u003e</string>

which present in string :

< Item Unknown >
Community
  • 1
  • 1
NguyenDat
  • 4,129
  • 3
  • 46
  • 46
  • Best answer to solve the problem < and > on android axml since &lt and &gt still not recognized as valid token – jace Jan 05 '18 at 02:48
  • 1
    That solution worked for me much better than above one. Thanks! – Radzik Jun 12 '18 at 10:06
4

I stumbled upon this question, as I use HTML markup in my strings.

If you refer to the Android String Resources documentation, in the section: "Styling with HTML markup" you will see that in strings.xml, you need to use &amplt; for an opening bracket, but you can safely use backslash and a closing bracket. For example:

<b>Text</b>

can be written as:

&lt;b>Text&lt;/b>

in your strings.xml.

If using this HTML string in your code, you can use:

Html.fromHtml( getResources().getString(R.string.yourHTMLString ) 

and the output will be your bold string!

Xebozone
  • 470
  • 6
  • 17