Android doesn't allow strings containing '?' because it has special meaning in XML context. How do I encode this symbol in a string resource?
Use a backslash.
Read more about escaping characters here.
Related
I am coding a maths app and I want to show special characters such as PI, E, or subscripts and all those things.
I want to show them on the xml file of the layout.
How can I do it?
Thank you guys for all!
You can use the Unicode value for the symbol, preceded by \u. For example, the pi character is "\u03C0"
This site: http://www.dionysia.org/html/entities/symbols.html has list of elements which can be used in xml. Just watch the second element. For example:
square = √
THen you need to conver it. For example:
String symbol = Html.fromHtml(square);
Alternative link is here: http://www.hrupin.com/2011/12/how-to-put-some-special-math-symbols-in-textview-editview-or-other-android-ui-element
The characters in a string resource are unicode. You can include special characters using the \unnnn notation.
There are many places to look up the unicode values on the web. Google found this one for me:
http://inamidst.com/stuff/unidata/
It gives a warning when we simply type in a string and not refer a string resource.
Why is this so ?
I mean shouldn't it be more efficient if the component simply puts in the text string then and there rather than having to search through dozens, probably 100s of strings in the XML string files ?
Why does eclipse insist on using string resource rather than a string ?
Putting the strings in a string resource allows your app to support multiple languages (by having a string resource file for each language you want to support).
It also prevents duplication in the case where you use the same string multiple times. You simply define it once in a string resource and refer it where ever you need to use it. If you later need to change its value, you only change it in one location (the string resource file).
Using string resources is great for special characters, such as theta, that aren't able to be in double quotes in your XML due to encoding.
Strings that need to be accessed multiple times throughout a project can be modified at one source.
It supports multiple languages.
I have a problem that I want to show a bulleted list contents which is resided in strings.xml file as an array elements. Then the problem is that how to convert the array elements in Html List format? Can any one suggest any solution regarding the same.
Thanks in advance
I just put the symbol directly into the strings.xml without any codes or anything:
<string name="msg_sms_no_note">• Notes and attachments will not be sent.</string>
There's a problem with the approach suggested by some of the answers in this thread of prepending the bullet unicode character (i.e. \u2022) to each of the Strings in the String array: You don't get proper indentation when one or more Strings in the String array span multiple lines. What you get is formatting as follows:
In order to get proper indentation, you're better using BulletSpan. In doing so, you'll get formatting as follows:
To use BulletSpan, you need to create a SpannableStringBuilder instance and append each String in your String array to this SpannableStringBuilder instance. As you append each String, call the setSpan(what:start:end:flags:) method on the SpannableStringBuilder instance passing in a BulletSpan instance for the what parameter. You can find an example of this in the appendBulletSpan(...) Kotlin extension function located here.
I think, the most elegant way of doing this is to load a WebView and put your string in it. this way, you use the common ul/li convention and you can style it at your leisure with CSS.
Use the unicode escape sequence "\u2022" in strings.xml
like so:
<string name="menu_new_trip_desc">View them in: \n\u2022 Table
I was wondering if I could place XML within /res/values/strings.xml? I ask this because I am checking for the XML data file for my application, if it does not exist yet then it creates it from the default contents that will be contained as a string resource.
Eclipse tries to change the less than and greater than tags to their corresponding HTML entities when using the GUI to edit the strings. Is eclipse on the right track? Because I should think that it will be written out into my file as HTML entities too. Could I use getText() rather than getString() to convert the entities back into tags?
Yes you can, just use CDATA
<string name="stringName1"><![CDATA[<html>bla</html>]]></string>
It will obviously not work unless you escape characters in there such as < or > or &.
If you do encode the XML, it should work fine but probably not the best way to do it. I would prefer binary resource.
For putting in string.xml, you may encode using
String encoded = URLEncoder.encode(xml);
And decoding is the opposite.
For binary, you place it in RAW folder and you get a binary stream and turn it to string and load.
I have done this way:
Put your string in strings.xml
<string name="my_string"><![CDATA[Your long text here]]></string>
How to use:
<TextView
android:id="#+id/textView"
android:text="#string/my_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Done
I have a strings.xml with a string-array resource. In another .xml I have a textview. setText() doesnt accept an array so how do I make my string array appear in the textview? I've done a search but can't seem to find something which addresses this issue. It is such a simple matter that I think I must be missing the obvious.
Ron
You can not refer to String Array resource in XML:
http://developer.android.com/guide/topics/resources/string-resource.html#StringArray
Check RESOURCE REFERENCE for both String and String Array resources. You can refer to String resource in XML, but you can not refer to String Array and Plurals resources.