hi I'm just creating a header for a app that uses twitter and just wanting to know how to show the # as text i put it in a string and it doesn't like it
<string name="twit">#evosdfresh</string>
then i call this string it doesn't work if i remove the # it works so its the # thats throwing it off
Try replacing the # char with its utf-8 xml decimal representation: #
EDIT: given the xml entity does not work, use the unicode definition: \u0040
If you have the # as the first character of the string, then you need to escape it with a backslash, e.g.
<string name="twit">\#evosdfresh</string>
If it's in the middle of the string you don't need the backslash.
Try # instead of #. If that doesn't work, you could also try escaping it, with 2 #'s instead of 1, or a backslash before it. ##evosdfresh or \#evosdfresh
My idea is to replace it with an #. Tell me if it works, I'd be interested to know, too ;)
Related
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'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"
How can I use escape characters in XML?
The situation is, I am a new android developer, and I need a string which need to be printed in 2 lines (other wise no space). This string is in string.xml file. I need to use /n line break character to break the string, but I don't know how to do it in XML file. Please help.
See reference http://developer.android.com/guide/topics/resources/string-resource.html#String
line break symbol is \n
/n - is wrong.
and you can write it in xml like it is.
Example:
<string name="multiline_text">line 1.\nline2.</string>
I'm not familiar with Android development but have you checked out CDATA? Refer to this link.
Basically, you wrap your string value like so:
<![CDATA[string value]]>
You can use double quotes:
<string name="mystring">"One line\nAnother line"</string>
You can use the character entity
to indicate a linefeed.
I'm trying to store a fully qualified url, with also query params:
www.miosito.net?prova®=bis
but it's causing a problem because ® is similar to ® entity and android tell me that and html entity is not well written.
I need this because every locale uses a fully different set of url query param.
I tried with [[CDATA[.. ]] but this syntax disliked by xml parser.
The problem is not with &req but with & itself. For XML/HTML you would have to use & entity (or &), but for URLs you should rather URL-encode (see docs) strings, and in that case said & should be replaced with %26. So your final string should look like:
www.miosito.net?prova%26reg=bis
Store it like this:
<string name="my_url">"www.miosito.net?prova®=bis"</string>
Where & is the XML equivelant of the ampersand symbol &.
Percent encoding may do the trick: http://en.wikipedia.org/wiki/Percent-encoding
You'll basically have something like this: www.miosito.net?prova%26reg=bis
You can enclose your url in double quotes, something like :
<string name="my_url">"www.miosito.net?prova®=bis"</string>
This is a recommended way to enclose string resources in Android.
Update 1 : Have a look at the following link for more info :
http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
Update 2:
#WebnetMobile.com : Correct, indeed :)
'&' is being treated a special character by xml and enclosing in quotes doesn't work. I tried out
www.miosito.net?prova%26reg=bis
and it didn't work out either. I even tried enclosing it in quotes but still didn't work. Am I missing something ?
Meanwhile, the following does work :
<string name="my_url">www.miosito.net%1$sprova%2$sreg=bis</string>
and then in code :
Resources resources=getResources();
String url=String.format(resources.getString(R.string.my_url),"?","&") ;
The '%1$s' and '%2$s' are format specifiers, much like what is used in printf in C. '%1$s' is for strings, '%2$d' is for decimal numbers and so on.
I need to be able to put a "-" in the string inside my strings.xml file.
My problem is that when I am putting my string which is "1261eba2-9d8c-11e1-93e3-40409e0f44a1", eclipse yells:
Multiple annotations found at this line:
- Replace "-" with an "en dash" character (–,
&;#8211;)
How can I fix this?
So, when you read the error message, your answer will be that
you have to replace - with –. Then it should work fine =)
http://en.wikipedia.org/wiki/Dash
The other answers are OK for when you want to display the string to the user. The user can't really tell the difference between a "real" dash and the unicode trickery.
But, if you really must have the dash (e.g. because that string is used as a password somewhere, or as a url key for an API) then you can just use this format:
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="TypographyDashes">
<string name="EVA_API_KEY">3c42b725-5e20-41c8-982f-dee40be8a05b</string>
</resources>
The warning will be removed and the string can be read using the regular:
getResources().getString(R.string.EVA_API_KEY);
Use back slash ( \ ) in front of every special character. like me\&android.
This called escape character. ( \ )
For hyphen use (-) (-)...
<string name="abc">Welcome - Bro...</string>
and For more symbol use below link
http://www.degraeve.com/reference/specialcharacters.php
Enjoy...
The dash is a punctuation mark that is similar to a hyphen or minus sign, but differs from both of these symbols primarily in length and function. The most common versions of the dash are the en dash (–) and the em dash (—), named for the length of a typeface's lower-case n and upper-case M respectively.
Reference
Just replace - with – because when you type a dash on the keyboard, XML reads dash as minus, that's all.
You probably have this:
<string name="test1">1261eba2-9d8c-11e1-93e3-40409e0f44a1</string>
But you need either one of these:
<string name="test2">1261eba2–9d8c–11e1–93e3–40409e0f44a1</string>
<string name="test3">1261eba2–9d8c–11e1–93e3–40409e0f44a1</string>
In the second one the - is replaced by a –. It's hard to tell the difference visually.
The quick fix shortcut in Eclipse is Ctrl + 1 by default and in Android Studio is Alt + Enter by default.
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'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"