I am trying to insert a "#" symbol at the beginning of a text field in android. I have specified that in the string resources file as
<string name="email_ext">#xyz.com</string>
But it throws a compiler error - "No resource type specified (at 'email_ext' with value '#xyz.com'). Is there a way to fix this? I have tried to set that using the setter, but I have different resource files for different languages. Thanks in advance.
Add a \ before that #
<string name="email_ext">\#xyz.com</string>
The reason is that an # is the open symbol of any reference across Android resources. You could point to a color using #color/white, if you defined it. Because of this, the compiler tries to allocate a resource named xyz.com, but it actually has not a type defined (such as drawable, layout, id, string, etc). Hence the compiler error.
In fact, there is a cleaner version (found here):
<string name="email_ext">"#xyz.com"</string>
Using quotes looks less hacky
Add \
<string name="email_ext">\#xyz.com</string>
I recommend you to use \uxxxx for special characters, this is the table of unicodes http://jrgraphix.net/research/unicode_blocks.php?block=0, in your case you can use \u0040 for #symbol something like this:
<string name="email_ext">\u0040xyz.com</string>
Regards!
Related
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>
Why put # in xml file of android to access some resources?
<android:background="#drawable/coloreffect">
why put before # in xml file of android to access some resources?
That is to distinguish a reference to a resource from other things.
For example, not only can you have android:background="#drawable/coloreffect", but you can have android:background="#ffff0000" to refer to a specific color. Not only can a TextView have android:text="#string/foo", but it can have android:text="Foo", for a hard-coded string. And so on.
It is used to differentiate normal strings and string referred to resources.
For example when you write
android:background="resource name without #"
then it act as static value it does not bind from your resources.
The special character # is used to indicate that the following string will reference a resource file.
If you don't put it then drawable/coloreffect is interpreted as a simple string.
Android follows different syntax for referring to resources, styles etc.
Resources can be referenced from resources using a special XML syntax, such as #drawable/myimage
To reference a style attribute, the name syntax is almost identical to the normal resource format, but instead of the at-symbol (#), use a question-mark (?), and the resource type portion is optional.
For more details https://developer.android.com/guide/topics/resources/accessing-resources.html
Hope this helps.
Example:
Android complains that ' ' is not a valid resource name character.
Am I forced to use say "New_Delhi" and then programatically map this in my program?
Yes, for the simple reason that Java fields cannot have spaces in their names. A string resource named New_Delhi is referenced in Java as R.string.New_Delhi. A space, in lieu of the underscore, would not be valid Java syntax.
<string name="weather_fragment_temp_str">%s°~%s°</string>
Multiple annotations found at this line:
- error: Multiple substitutions specified in non-positional format; did you mean to add the formatted="false"
attribute?
- error: Unexpected end tag string
The reason for this is that you're specifying that you want to substitute multiple values into this string, however you need to specify where each argument should go. The reason for this is that different languages are structured differently.
You can use string substitution with multiple substitutions like so:
<string name="weather_fragment_temp_str">%1$s°~%2$s°</string>
This way, other languages can have the replacements in different locations. For example you may want 16°~20° in English, and 20°~16° in Japanese - in your Japanese strings.xml you'd just do %2$s°~%1$s°, and then your code stays the same (that's an entirely contrived example, of course).
How would one use an # symbol inside a string in strings.xml?
<string name="twitter">#npike</string>
The XML editor gets rather angry:
error: Error: No resource type specified (at 'twitter' with value '#npike').
Use this instead :
<string name="twitter">\#npike</string>
I would suggest use unicode like \u0040 instead of '#' symbol. XML processing doesn't like special symbols. Here is list of special characters and it's unicode valuesSpecial characters and unicode values
Try escaping it with a backslash "\#". That works for quotes. You can't use XML entities in strings.xml, otherwise I would recommend that.