Home » Android » java – How to have "&" i.e and symbol in Android TextView

java – How to have "&" i.e and symbol in Android TextView

Posted by: admin April 23, 2020 Leave a comment

Questions:

I want to have some simple text in textView..

Like…(android & java)

means I exactly require “and” symbol in textView

If I’m providing within TextView android:text="Bakeries & Dessert" in textView it’s showing error in xml.

Can Anyone suggest me how to ignore special case characters & ,” etc and have them in our TextView text.

How to&Answers:

Try to set the text by code

TextView tv = (TextView)findViewById(R.id.textview);
tv.setText("input");

using Html to encode/decode the text

Html.fromHtml(string) 

Answer:

The layout files are just xml and the & symbol is not allowed by itself.

You will need to use & instead

http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Predefined_entities_in_XML

Answer:

Use this :

   &lt; for < 
   &gt; for > 
   &amp; for &

Example:

  <string name="lessthan"> &lt; teste &gt;</string>
  <string name="link"> Puzzles &amp; code </string>

Answer:

The layout files are just xml and the & symbol is not allowed by itself.

You will need to use & instead of &

This will work fine

  <string name="contest_giveaways" formatted="false">Contests &amp; Giveaways</string>

Answer:

Another way to solve this is using Unicode:

  1. set your text in your xml to

    android:text="@string/myNameHere"

  2. In strings xml add the following line:

    <string name="myNameHere">bakeries \u0026 desserts</string>

voilá!

Answer:

Simply write it with amp followed by semi-colon as below:

android:text="Bakeries &amp; Dessert"

Answer:

Define a new string in strings.xml with the value of “Bakeries & Dessert”.

Then, in the layout declare

<TextView android:text="@string/your_string_value"></TextView>

Later edit:

Adding an invalid character for XML in another XML file to circumvent the limitation will obviously not work. However, if you use Eclipse, when you add a new value in strings.xml, the characters like &, < and > will be automatically replaced by the HTML equivalents (&amp; &lt; &gt;).

Answer:

The layout file is an XML file and has certain restrictions on which characters may be used. However, you can use XML escape sequences to display characters which have special meaning to XML. In this case, replace the “Bakeries & Dessert” with the string “Bakeries &amp; Dessert” and you should get the behaviour that you require.