Problem with & and double quotes in my Android app - android

in my app I am facing following two problems
I am trying to show some text from strings.xml file stored in res/values folder. The problem is whenever I enter the & it shows an error as follows:
Multiple annotations found at this line:
- The entity name must immediately follow the '&' in the entity
reference.
- error: Error parsing XML: not well-formed (invalid token)
When I remove it the error gets cleared. how to use such special characters in my app
When I click a button in my app it hits an url and in return I am getting an return data as follows
<img src="http://xxxxxxxxxxxxxxxxxxx/test.php%3Fcode_hash%3Da8c159f35af&guid=ON"/>
I want to get this url removing the img src="
I tried using the replacewith("","");
But here I am unable to give the " within the double quotes.
How to solve the above issues?

In XML strings for & (ampersand) you need to use & and quotes (single or double) need to be escaped,
<string name="with_amp">I, Me & Myself</string>
<string name="with_quotes>Single quote \' and Double quotes \"</string>

Instead of & you have to use & check in the following statements
test&
<string name="url">http://xxxxxxxxxxxxxxxxxxx/test.php%3Fcode_hash%3Da8c159f35af&guid=ON</string>

To use the symbol & in your strings.xml file, you need to use the appropriate XML character entity.
For ampersand, use &.

Related

Android studio won't change string

In my strings.xml file I had this string:
<string name="lets_do_this">Let's Do This</string>
This gave me an error of:
Error:(897) Apostrophe not preceded by \ (in Let's Do This)
So I changed the string to this:
<string name="lets_do_this">Let\'s Do This</string>
But every time I build the project, the string keeps changing back to the first version for some reason and I keep getting that error without the ability to change the string. Why is this happening?
If it changes back, sounds like you changed the strings xml file that is located within the build folder, but you need to edit your own within res/values of the app module.
If you have an apostrophe (') in your string, you must either escape it with a backslash (\') or enclose the string in double-quotes ("").
see
Formatting and Styling - String Resources
<string name="lets_do_this">"Let's Do This"</string>

How to overcome the fault in apostrophe strings.xml in android? [duplicate]

I have declared a long string in string.xml of an application.
Declared like this
<string name="terms">PLEASE READ THESE TERMS OF USE CAREFULLY BY ACCESSING THIS .................</string>
But this gives the following error :
error: Apostrophe not preceded by \ (in PLEASE READ THESE TERMS OF USE CAREFULLY
post your complete string. Though, my guess is there is an apostrophe (') character in your string. replace it with (\') and it will fix the issue. for example,
//strings.xml
<string name="terms">
Hey Mr. Android, are you stuck? Here, I\'ll clear a path for you.
</string>
Ref:
http://www.mrexcel.com/forum/showthread.php?t=195353
https://code.google.com/archive/p/replicaisland/issues/48
Solution
Apostrophes in the strings.xml should be written as
\'
Example
In my case I had an error with this string in my strings.xml and I fixed it.
<item>Most arguments can be ended with three words, "I don\'t care".</item>
Here you see my app builds properly with that code.
Here is the actual string in my app.
https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
Escaping apostrophes and quotes
If you have an apostrophe or a quote in your string, you must either escape it or enclose the whole string in the other type of enclosing quotes. For example, here are some stings that do and don't work:
<string name="good_example">"This'll work"</string>
<string name="good_example_2">This\'ll also work</string>
<string name="bad_example">This doesn't work</string>
<string name="bad_example_2">XML encodings don&apos;t work</string>
You have to put \ before an apostrophe. Like this \' , Also check that you are editing strings.xml and not values.xml (android studio directs you to this file when shows the error). Because if you edit values.xml and try to compile again, the error persists. This was happening to me recently.
Use this regex (?<!\\)' for searching an unescaped apostrophe.
It finds an apostrophe that not preceded by a backslash.
I use hebrew(RTL language) in strings.xml.
I have manually searched the string.xml for this char: '
than I added the escape char \ infront of it (now it looks like \' ) and still got the same error!
I searched again for the char ' and I replaced the char ' with \'(eng writing) , since it shows a right to left it looks like that '\ in the strings.xml !!
Problem solved.
The best answer of simply escaping the ' with \' works but I do not like the solution because when using replace all with ' to \', it only works once. The second time you'll be left with \\'.
Therefore, use: \u0027.
You may be able to use unicode equivalent both apostrophe and other characters which are not supported in xml string. Apostrophe's equivalent is "\u0027" .
1. for error: unescaped apostrophe in string
what I found is that AAPT2 tool points to wrong row in xml, sometimes.
So you should correct whole strings.xml file
In Android Studio, in problem file use :
Edit->find->replace
Then write in first field \' (or \&,>,\<,\")
in second put '(or &,>,<,")
then replace each if needed.(or "reptlace all" in case with ')
Then in first field again put '(or &,>,<,")
and in second write \'
then replace each if needed.(or "replace all" in case with ')
2. for other problematic symbols
I use to comment each next half part +rebuild
until i won't find the wrong sign.
E.g. an escaped word "\update" unexpectedly drops such error :)
To Find all the ' in your file, do the following:
Ctrl + F [Enter in the search field apostrophe] '
' should be written as "World's"

Error in strings.xml file in Android

I have declared a long string in string.xml of an application.
Declared like this
<string name="terms">PLEASE READ THESE TERMS OF USE CAREFULLY BY ACCESSING THIS .................</string>
But this gives the following error :
error: Apostrophe not preceded by \ (in PLEASE READ THESE TERMS OF USE CAREFULLY
post your complete string. Though, my guess is there is an apostrophe (') character in your string. replace it with (\') and it will fix the issue. for example,
//strings.xml
<string name="terms">
Hey Mr. Android, are you stuck? Here, I\'ll clear a path for you.
</string>
Ref:
http://www.mrexcel.com/forum/showthread.php?t=195353
https://code.google.com/archive/p/replicaisland/issues/48
Solution
Apostrophes in the strings.xml should be written as
\'
Example
In my case I had an error with this string in my strings.xml and I fixed it.
<item>Most arguments can be ended with three words, "I don\'t care".</item>
Here you see my app builds properly with that code.
Here is the actual string in my app.
https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
Escaping apostrophes and quotes
If you have an apostrophe or a quote in your string, you must either escape it or enclose the whole string in the other type of enclosing quotes. For example, here are some stings that do and don't work:
<string name="good_example">"This'll work"</string>
<string name="good_example_2">This\'ll also work</string>
<string name="bad_example">This doesn't work</string>
<string name="bad_example_2">XML encodings don&apos;t work</string>
You have to put \ before an apostrophe. Like this \' , Also check that you are editing strings.xml and not values.xml (android studio directs you to this file when shows the error). Because if you edit values.xml and try to compile again, the error persists. This was happening to me recently.
Use this regex (?<!\\)' for searching an unescaped apostrophe.
It finds an apostrophe that not preceded by a backslash.
I use hebrew(RTL language) in strings.xml.
I have manually searched the string.xml for this char: '
than I added the escape char \ infront of it (now it looks like \' ) and still got the same error!
I searched again for the char ' and I replaced the char ' with \'(eng writing) , since it shows a right to left it looks like that '\ in the strings.xml !!
Problem solved.
The best answer of simply escaping the ' with \' works but I do not like the solution because when using replace all with ' to \', it only works once. The second time you'll be left with \\'.
Therefore, use: \u0027.
You may be able to use unicode equivalent both apostrophe and other characters which are not supported in xml string. Apostrophe's equivalent is "\u0027" .
1. for error: unescaped apostrophe in string
what I found is that AAPT2 tool points to wrong row in xml, sometimes.
So you should correct whole strings.xml file
In Android Studio, in problem file use :
Edit->find->replace
Then write in first field \' (or \&,>,\<,\")
in second put '(or &,>,<,")
then replace each if needed.(or "reptlace all" in case with ')
Then in first field again put '(or &,>,<,")
and in second write \'
then replace each if needed.(or "replace all" in case with ')
2. for other problematic symbols
I use to comment each next half part +rebuild
until i won't find the wrong sign.
E.g. an escaped word "\update" unexpectedly drops such error :)
To Find all the ' in your file, do the following:
Ctrl + F [Enter in the search field apostrophe] '
' should be written as "World's"

android - string.xml parsing error

i have made a string named info_data as follows
<string name="info_address">SVG Service Verlags GmbH & Co. KG\n
Schwertfegerstra?e 1-3\n
D-23556 L?beck\n
</string>
i am getting an xml error saying that
Multiple annotations found at this line:
- The entity name must immediately follow the '&' in the entity
reference.
- error: Error parsing XML: not well-formed (invalid token)
this error comes on the first line containing the &.
what is going wrong and how do i fix it.
thank you in advance.
You can't use the & sign. Try & (I haven't tested it but it may work).
I believe you will need to change the & to &.
<string
name="info_address">SVG Service Verlags GmbH & Co. KG\n Schwertfegerstra?e 1-3\n
D-23556 L?beck\n</string>
copy paste the code i have pasted.
use this string use & instead of &
While other answers about the ampersand may be correct, I don't think its just that.
This should be of some use.
Where an attribute value is a string, double backslashes ('\') must be used to escape characters — for example, '\n' for a newline or '\uxxxx' for a Unicode character.
You have \n, which may need to be \\n in addition to the change to the &

writing some characters like '<' in an xml file

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.
Use
< for <
> for >
& for &
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 >
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:
<b>Text</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!

Categories

Resources