How to put a "-" in string.xml file - android

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 (&#45) (-)...
<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.

Related

How to put a “-” in string.xml file, but without escaping

I have a configuration.xml file which I hold all the (yep, you guessed it!).. configuration strings and values and stuff.
One of those values is a string which is an oauth client id, and it has a hyphen..
<string name="server_clientid">5467656-blahblahblah.apps.googleusercontent.com</string>
Now I get the warning message..
Replace "-" with an "en dash" character (–, –) ?
Ok fair enough, but if I escape with this then the client id is not valid when I fetch it within the app. I can't use & #8211; basically. How do I get around this?
You wrap your values in
<![CDATA[ ]]>
which stops the parser from parsing the contents. E.g.
<string name="server_clientid"><![CDATA[ 5467656-blahblahblah.apps.googleusercontent.com ]]></string>
You can also suppress the lint warning (which is false since the dash is not used in a typographical context):
<resources xmlns:tools="http://schemas.android.com/tools">
...
<string name="server_clientid" tools:ignore="TypographyDashes">5467656-blahblahblah.apps.googleusercontent.com</string>
The "n dash" (–, –) and the "m dash" (—, —)
characters are used for ranges (n dash) and breaks (m dash). Using
these instead of plain hyphens can make text easier to read and your
application will look more polished.
Replace smaller "-" with bigger "–". You are good to go.

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"

How to use escape characters in XML

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.

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"

How to keep the spaces at the end and/or at the beginning of a String?

I have to concatenate these two strings from my resource/value files:
<string name="Toast_Memory_GameWon_part1">you found ALL PAIRS ! on </string>
<string name="Toast_Memory_GameWon_part2"> flips !</string>
I do it this way :
String message_all_pairs_found = getString(R.string.Toast_Memory_GameWon_part1)+total_flips+getString(R.string.Toast_Memory_GameWon_part2);
Toast.makeText(this, message_all_pairs_found, 1000).show();
But the spaces at the end of the first string and at the beginning of the second string
have disappeared (when the Toast is shown) ...
What should I do ?
I guess the answer is somewhere here in this documentation link
or is it something like using &amp ; for the "&" character ??
Even if you use string formatting, sometimes you still need white spaces at the beginning or the end of your string. For these cases, neither escaping with \, nor xml:space attribute helps. You must use HTML entity   for a whitespace.
Use   for non-breakable whitespace.
Use for regular space.
I ran into the same issue. I wanted to leave a blank at the end of a resource string representing an on-screen field name.
I found a solution on this issue report : https://github.com/iBotPeaches/Apktool/issues/124
This is the same idea that Duessi suggests. Insert \u0020 directly in the XML for a blank you would like to preserve.
Example :
<string name="your_id">Score :\u0020</string>
The replacement is done at build time, therefore it will not affect the performance of your game.
This documentation suggests quoting will work:
<string name="my_str_spaces">" Before and after? "</string>
I just use the UTF code for space "\u0020" in the strings.xml file.
<string name="some_string">\u0020The name of my string.\u0020\u0020</string>
works great. (Android loves UTF codes)
This question may be old, but as of now the easiest way to do it is to add quotation marks.
For example:
<string name="Toast_Memory_GameWon_part1">"you found ALL PAIRS ! on "</string>
<string name="Toast_Memory_GameWon_part2">" flips !"</string>
There is possible to space with different widths:
<string name="space_demo">| | | ||</string>
| SPACE | THIN SPACE | HAIR SPACE | no space |
Visualisation:
use "" with the string resource value.
Example :
<string>"value with spaces"</string>
OR
use \u0020 code for spaces.
If you really want to do it the way you were doing then I think you have to tell it that the whitespace is relevant by escaping it:
<string name="Toast_Memory_GameWon_part1">you found ALL PAIRS ! on\ </string>
<string name="Toast_Memory_GameWon_part2">\ flips !</string>
However, I'd use string formatting for this. Something like the following:
<string name="Toast_Memory_GameWon">you found ALL PAIRS ! on %d flips !</string>
then
String message_all_pairs_found = String.format(getString(R.string.Toast_Memory_GameWon), total_flips);
Working well
I'm using \u0020
<string name="hi"> Hi \u0020 </string>
<string name="ten"> \u0020 out of 10 </string>
<string name="youHaveScored">\u0020 you have Scored \u0020</string>
Java file
String finalScore = getString(R.string.hi) +name+ getString(R.string.youHaveScored)+score+ getString(R.string.ten);
Toast.makeText(getApplicationContext(),finalScore,Toast.LENGTH_LONG).show();
Screenshot
here Image of Showing Working of this code
All answers here did not work for me. Instead, to add a space at the end of a string in XML i did this
<string name="more_store">more store<b> </b> </string>
An argument can be made for adding the space programmatically. Since these cases will be often used in concatenations, I decided to stop the madness and just do the old + " " +. These will make sense in most European languages, I would gather.
I've no idea about Android in particular, but this looks like the usual XML whitespace handling - leading and trailing whitespace within an element is generally considered insignificant and removed. Try xml:space:
<string name="Toast_Memory_GameWon_part1" xml:space="preserve">you found ALL PAIRS ! on </string>
<string name="Toast_Memory_GameWon_part2" xml:space="preserve"> flips !</string>
This may not actually answer the question (How to keep whitespaces in XML) but it may solve the underlying problem more gracefully.
Instead of relying only on the XML resources, concatenate using format strings.
So first remove the whitespaces
<string name="Toast_Memory_GameWon_part1">you found ALL PAIRS ! on</string>
<string name="Toast_Memory_GameWon_part2">flips !</string>
And then build your string differently:
String message_all_pairs_found =
String.format(Locale.getDefault(),
"%s %d %s",
getString(R.string.Toast_Memory_GameWon_part1),
total_flips,
getString(R.string.Toast_Memory_GameWon_part2);
Toast.makeText(this, message_all_pairs_found, 1000).show();
There is also the solution of using CDATA. Example:
<string name="test"><![CDATA[Hello world]]></string>
But in general I think \u0020 is good enough.
If you need the space for the purpose of later concatenating it with other strings, then you can use the string formatting approach of adding arguments to your string definition:
<string name="error_">Error: %s</string>
Then for format the string (eg if you have an error returned by the server, otherwise use getString(R.string.string_resource_example)):
String message = context.getString(R.string.error_, "Server error message here")
Which results in:
Error: Server error message here
It does not work with xml:space="preserve"
so I did it the quickest way =>
I simply added a +" "+ where I needed it ...
String message_all_pairs_found = getString(R.string.Toast_Memory_GameWon_part1)+" "+total_flips+" "+getString(R.string.Toast_Memory_GameWon_part2);

Categories

Resources